upload image with wordpress rest api

I am having trouble uploading an image with the WordPress REST API (WP-API). Can someone provide some sample code. Basically I have an image that I got from a service and I’m showing in an <img> tag. Now I need to take that image and save it in the media library using javascript.

Alternatively (not using WP-API) if there is a plugin that would do that, I can pass the url for the image to get is saved in the media library.

Read More

Thanks

Related posts

Leave a Reply

1 comment

  1. You can use the wp_insert_attachment if API is not preferred. Source

    Ignore the $parent_post_id if the media library is not post specific.

    You can retrieve the image url using this trick: Retrieve images and then use the variable for $filename

    // $filename should be the path to a file in the upload directory.
    $filename = '/path/to/uploads/2013/03/filename.jpg';
    
    // The ID of the post this attachment is for.
    $parent_post_id = 37;
    
    // Check the type of file. We'll use this as the 'post_mime_type'.
    $filetype = wp_check_filetype( basename( $filename ), null );
    
    // Get the path to the upload directory.
    $wp_upload_dir = wp_upload_dir();
    
    // Prepare an array of post data for the attachment.
    $attachment = array(
     'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ), 
     'post_mime_type' => $filetype['type'],
     'post_title'     => preg_replace( '/.[^.]+$/', '', basename( $filename ) ),
     'post_content'   => '',
     'post_status'    => 'inherit'
    );
    
    // Insert the attachment.
    $attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
    
    // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    
    // Generate the metadata for the attachment, and update the database record.
    $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
    wp_update_attachment_metadata( $attach_id, $attach_data );