Simple Way of Retrieve And Upload an Image from URL on WordPress Programmatically

Seegatesite , how to retrieve and upload an image from URL on WordPress programmatically. If you are a programmer WordPress theme and plugins sometimes need a script to download the images from a third-party URL. For example, when you are creating the tools to extract an image from the Amazon API or Flickr and want to automatically upload the image to your WordPress uploads folder and set it as a thumbnail. You don’t have to worry because WordPress already prepare the Class and Function to use.

The whole Class and Function, in order to use WordPress’s features, are already documented on wordpress.org, but as a beginner like me, sometimes still confused to implement these functions in order to run properly. So on this occasion, I would like to share a simple way of retrieve and upload an image from URL on WordPress programmatically.

Some of the WordPress’s functions which are used to retrieve and upload an image from URL are as follows

  1. wp_remote_get

    Wp Remote Get Simple Way Of Retrieve And Upload An Image From URL On WordPress Programmatically
    This function is used to retrieve the RAW image with the GET method. The results of RAW files are HTTP header and content. by default the use of wp_remote_get as follows :

    If you want to use a simple way can be in the following ways :

    $response = wp_remote_get( 'image_url', array( 'timeout' => 8 ) )

  2. wp_remote_retrieve_body

    This function is used to get the body content from an RAW file / response. Once you get RAW files using the wp_remote_get function, the next step is taking body content of the RAW file.

    The default usage of wp_remote_retrieve_body as follows :

    $body = wp_remote_retrieve_body($response)

  3. wp_upload_bits

    Wp Upload Bits Simple Way Of Retrieve And Upload An Image From URL On WordPress Programmatically
    This function is used to create the files from the raw body that already obtained from wp_remote_retrieve_body . The default usage of wp_upload_bits as follows :

    wp_upload_bits( $name, $deprecated, $bits, $time )

    Response from wp_upload_bits as an array of the following key.

    file – The location file uploads
    URL – URL file uploads
    type – Type of files that have been uploaded
    error – Description error if it occurs

    Upload Image Successfully Simple Way Of Retrieve And Upload An Image From URL On WordPress Programmatically

  4. wp_insert_attachment

    This function is used to add/register the files we’ve uploaded into the media library. The default usage of wp_insert_attachment as follows :

    wp_insert_attachment( $attachment, $filename, $parent_post_id )

    The results of the response of the wp_insert attachment is an attachment ID.

  5. add_post_meta

    This is an additional function if you want to use the image as a thumbnail image on the theme.

    The default usage of add_post_meta for thumbnail image as follows :

    add_post_meta($post_id, '_thumbnail_id', $attachment_id, true)

With the 5 function above you have managed to retrieve and upload images from third-party URL / server on WordPress then set the image as a thumbnail on the theme. Simple example as follows:

Thus article about the simple way of retrieve and upload an image from URL on wordPress programmatically, hope useful.

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.