Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $username = "ZX";
- $password = "ZX";
- $host = 'http://ZX.com/wp-json/wp/v2/media';
- $data = json_encode($data);
- $file = '/Users/xx.png';
- $imagedata = file_get_contents($file);
- $process = curl_init($host);
- curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($process, CURLOPT_BINARYTRANSFER, TRUE);
- curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($process, CURLOPT_TIMEOUT, 50);
- curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
- curl_setopt($process, CURLOPT_POSTFIELDS, $data);
- curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type:image/png','Content-Disposition:attachment;filename='.$file));
- $return = curl_exec($process);
- print_r($return);
- $data = array(
- "status" => "draft",
- "title" => "Photo media",
- "description" => "Photo media1",
- "media_type" => "image",
- "alt_text" => "alternate text"
- );
- $media = media_sideload_image(
- 'http://i.imgur.com/bcJvAj0.jpg',
- $post->ID,
- 'Some image description',
- 'src'
- );
- if ( ! empty( $media ) and ! is_wp_error( $media ) ) {
- // reference new image to set as featured
- $attachments = get_posts( [
- 'post_type' => 'attachment',
- 'posts_per_page' => 1,
- 'post_status' => 'any',
- 'post_parent' => $post->ID,
- ] );
- if ( is_array( $attachments ) ) {
- set_post_thumbnail( $post->ID, $attachments[0]->ID );
- }
- // Test print our image. The return value is the src of the sideloaded image.
- printf( '<img src="%s" />', $media );
- }
Add Comment
Please, Sign In to add comment