
The following snippet supports the article on this page titled “Import Remote Images into WordPress and Return Image URL, Image ID, and Image Dimensions”. It will import a remote image into the WordPress media library, create all the relevant image sizes, and return the local image link or array of image data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
/* Code to demonstrate the beliefmedia_import_image() function Import Remote Images into WordPress and Return Image URL, Image ID, and Image Dimensions http://www.beliefmedia.com/import-remote-images-wordpress http://www.beliefmedia.com/code/wp-snippets/import-remote-image-demo */ function beliefmedia_import_image_demo($atts) { extract(shortcode_atts(array( 'url' => '', /* Image URL */ 'description' => 'Image', 'source' => 0 ), $atts)); /* Post ID */ global $post; /* DB Image hash */ $hash = 'wp_imp_' . md5($url); if ( (get_post_meta($post->ID, $hash, true)) != '') { $temp = get_post_meta($post->ID, $hash, true); } else { $temp = beliefmedia_import_image($url, $description, $src = 0); add_post_meta($post->ID, $hash, $temp); } /* Return image string or array? */ $result = ($source) ? (string) $temp['html'] : 'pre' . print_r($temp, true) . '/pre'; return $result; } add_shortcode('importimagedemo', 'beliefmedia_import_image_demo'); |
The function is for demonstration purposes only and will require modification before it can be applied in any practical way. That said, you can use it to return the image as follows:
1 |
<img src='[importimagedemo url="http://www.flight.org/wp-content/themes/wt_spirit/images/logo.png" description="Image Imported from Flight" source="1"]'> |
The result:
Of course you’ll also need our beliefmedia_import_image()
function. The source article provides further detail.
Shortt URL for this post: http://shor.tt/17T4