Viewing 6 replies - 1 through 6 (of 6 total)
  • It’s on the documentation

    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.

    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…

    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.

    Plugin Author Ryan McCue

    (@rmccue)

    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,
    }
    

    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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘New post with image’ is closed to new replies.