How to create new post with photo attached in WordPress using XMLRPC?

前端 未结 6 2267
忘了有多久
忘了有多久 2020-12-15 13:44

Anyone knows how to create new post with photo attached in WordPress using XMLRPC?

I am able to create new post and upload new picture separately,

6条回答
  •  忘掉有多难
    2020-12-15 14:02

    Here's some sample code to attach an image from a path not supported by WordPress (wp-content)

     value pairs
        # echo $uploads['basedir'] . '
    '; $productpicture = str_replace('/uploads','',$productpicture); $localfile = $uploads['basedir'] .'/' .$productpicture; # echo "Local path = $localfile \n"; if (!file_exists($filename)) { echo "hittade inte $filename !"; die ("no image for flaska $id $newid !"); } if (!copy($filename, $localfile)) { wp_delete_post($newid); echo "Failed to copy the file $filename to $localfile "; die("Failed to copy the file $filename to $localfile "); } $wp_filetype = wp_check_filetype(basename($localfile), null ); $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\.[^.]+$/', '', basename($localfile)), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $localfile, $newid ); // you must first include the image.php file // for the function wp_generate_attachment_metadata() to work require_once(ABSPATH . 'wp-admin/includes/image.php'); $attach_data = wp_generate_attachment_metadata( $attach_id, $localfile ); wp_update_attachment_metadata( $attach_id, $attach_data ); } ?>

提交回复
热议问题