and like us on FB

The WordPress function below – intended to be referenced inside another shortcode function – will import a remote image into your WordPress library. WordPress has a function to handle importing remote images, but I needed the image ID and dimensions, and our function provides for this. Because it’s difficult to show the result of our code, I’ve written a shortcode that serves no real purpose other than to demonstrate how the primary function is used.

The function will likely be referenced by other posts in the future.

The Result

Consider the image header to our neglected FLIGHT website. Our function will import the logo into our WordPress library (inheriting our normal image structure), create all our defined image sizes, query the database for the image ID, and finally, query the database again for the image width and height. Once you retrieve image details it’s very important that you record the result in your WP database. Calling the function again will result with the image importing again and again.

Usage is as follows:

Using the demonstration function provided on this page, and assuming we’re returning an array rather than just the local URL, we’ll return the following:

Array
(
    [html] => http://www.beliefmedia.com/wp-content/uploads/2017/09/logo.png
    [url] => http://www.flight.org/wp-content/themes/wt_spirit/images/logo.png
    [imgid] => 10954
    [width] => 420
    [height ] => 90
)

The linked example function is for demonstration purposes only and will require modification before it can be applied in any practical way.

WordPress Function

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

In almost all cases we’ll return an array and store that data in the WP database.

A Practical Example

We’ll be referencing this snippet when performing tasks such as listing YouTube videos in a tower with accompanying thumbnails. If you choose to store those images locally, you may want to import them into your own WordPress library. As an example of how we might retrieve the remote image, import it into WP, and then return a specific size, consider the following example.

We’ll import a YouTube image JPG Image associated with one of our FLIGHT videos . The shortcode of [remoteimagedemo url="https://img.youtube.com/vi/vbxl3hc_pL8/0.jpg"] will return the following thumbnail .

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

In reality we’d create a means to strip the black bars, we’d apply appropriate image styling and tags, and we would return a default image if wp_get_attachment_image_src() returned false (along with other some other basic error checking). Because the whole exercise is mildly expensive, when we perform this operation for our own (upcoming) YouTube tower code, we cache the resulting HTML with Simple Cache or via the transient API.

Considerations

  • Our primary image shortcode (and that which we advocate for our clients) is performed via an image ID, and the resulting image is sized and cached outside of the WP library. If you’re currently a partner, continue to use the methods as described in your Custom WordPress Documentation.
  • To create custom image sizes, use WP’s add_image_size() function. If you were to use a YouTube thumbnail, you should create an image size with an aspect ratio of 16:9 (1.777).

Download

Title: Import Remote Images into WordPress
Description: Import Remote Images into WordPress and Return Image URL, Image ID, and Image Dimensions.
Download: Shortcode (V0.2) | | Plugin Page

Shortt URL for this post: http://shor.tt/1TM7