WordPress.org

Ready to get started?Download WordPress

Forums

JSON REST API (WP API)
[resolved] New post with image (7 posts)

  1. giovani.schram
    Member
    Posted 5 months ago #

    How can I create a new post with an image attached?

    https://wordpress.org/plugins/json-rest-api/

  2. slpixe
    Member
    Posted 3 months ago #

    It's on the documentation

  3. dunar21
    Member
    Posted 3 months ago #

    No, it's not in the documentation. It's in the code, however. It seems the author did not want to handle post creation alongside media upload and attachment. So, he separated the two requiring one to be done, then the other:

    /**
     * Upload a new attachment
    *
    * Creating a new attachment is done in two steps: uploading the data, then
    * setting the post. This is achieved by first creating an attachment, then
    * editing the post data for it.
    *
    * @param array $_files Data from $_FILES
    * @param array $_headers HTTP headers from the request
    * @return array|WP_Error Attachment data or error
    */

    This is however rather obscure in the code because I do not find anywhere to actually assign the post id to new media.

    I guess there is the ability to update_post_meta($post_id, '_thumbnail_id', $image_id);. So after getting the media data back, you can assign a featured image by updating the post meta data with the previously returned media attachment id.

    You can also hook into the json_insert_post to do some messing around with the data after the post has been created (thank you author for finally adding this needed hook). Keep in mind, though, that this hook fires off AFTER the meta is added and THERE ARE NO HOOKS WITHIN THE "handle_post_meta_action" function AND THERE SHOULD BE!!!! :).

    There are two methods, attach_thumbnail and add_thumbnail_data, neither of which appear to be used anywhere.

    So, you can add some extra fields to your post data, call the hook above and handle your media attachments as you wish, relative to that specific post you are handling. You could extend the custom post type class and do some damage there and on that same thought you can extend the api and create your own class with custom routes and do whatever you want in there. You can then call the media class' attach_thumbnail method to set a post thumbnail if you want, etc... etc... It's not already done for you necessarily and it will take a few steps, but it's doable.

  4. dunar21
    Member
    Posted 3 months ago #

    RE: "THERE ARE NO HOOKS WITHIN THE "handle_post_meta_action" function AND THERE SHOULD BE!!!! :). " I may be incorrect, there are hooks in the get_meta method that might be able to utilize...

  5. dunar21
    Member
    Posted 3 months ago #

    Ok, so in order to attach a featured image to a new post, you should first create the media attachment. You do that by either sending your post data to /media (I'm using upload from data, you can also send upload from file data - probably url with an @ at the beginning of the param -). If you are posting raw data, you have to allow unfiltered uploads capabilities in your wp_config file like so:

    define( 'ALLOW_UNFILTERED_UPLOADS', true );

    Then you have to collect the file data:

    $filename = $_FILES['uploadedfile']['tmp_name'];
    $finfo = new finfo(FILEINFO_MIME_TYPE);
    $fmime = $finfo->file($filename);
    $handle = fopen($filename, "r");
    $fdata = fread($handle, filesize($filename))

    ;

    Then pass the data via curl (if you're using curl):

    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:".$fmime, 'Content-Disposition: attachment; filename="' .$_FILES['uploadedfile']['name'] . '"', "Accept:application/json"...

    and post that data:

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fdata);

    If all is well, you should get a 201 created response status.

    The returned data will include the new media attachment id:

    $attachnfo = json_decode($result);
    echo $attachnfo->ID;

    Check for that status and if returned, go about creating a new post. To make sure the new post has the featured image set, make sure your params include a post_meta array with the meta key _thumbnail_id set to the returned ID of the successful attachment.

  6. Ryan McCue
    Member
    Plugin Author

    Posted 2 months ago #

    To clarify what's been said above, uploading attachments is a separate step to setting a featured image. The documentation covers media creation, which is a two-step process: first, POST the image data to /media, which will create the post; then, update the returned post with any necessary attachment post data. The first step will give you a Post entity which will include:

    {
        "ID": 4,
    }
    

    As for setting this as the featured image on a post, you can then set this on a post via the featured_image value, which should be the image ID:

    {
        "featured_image": 4,
    }
    
  7. dunar21
    Member
    Posted 2 months ago #

    Ok Ryan, I see that you hooked into pre insert:

    add_filter( 'json_pre_insert_post', array( $wp_json_media, 'preinsert_check' ), 10, 3 );

    in plugin.php. So after creating the media and getting the id, instead of adding a post_meta key of _thumbnail_id, you would just add the param "featured_image" with the previously returned media attachment ID and it will filter through to the set_post_thumbnail method.

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.