WordPress hook that gets featured image from direct URL?

I use a lot of plugins on my site so I’m trying to minimize the amount more plugins.

I was wondering if there’s a wordpress hook I can use so that I can use an image that is hosted on imageshack and use that as a featured image without having the image saved on my wordpress site?

Is that even possible? This sounds like a really complicated request. lol

1 Answer
1

First try: A bad one

Have you tried this solution? With a few adaptions it should work for your question too.

Second try: A real approach with a solution!

If you want to use an URL based featured image, without saving the image file in your WordPress installation, but still use the implemented WordPress functions within your Theme/Plugin, you need to know how media in WP is represented:

When you upload a file, WordPress does not reference the image or file
using its URL, it uses an attachment. Attachments are posts of type
attachment, and are referred to by their post ID. For example, when
you set a featured image on a post, it stores your chosen image’s post
ID in that post’s meta _thumbnail_id.

Source: http://www.wptherightway.org/en/post_types/index.html (2016-03-14 01:23 UTC)

Conclusion: URL based featured images are not foreseen in the WP enviroment.

If you still wanna achieve it, there are 3 simple steps:

  1. Filter the wp_get_attachment_image_src() result, to allow the url strings as _thumbnail_id instead of post_id.

    function my_get_attachment_image_src($image, $attachment_id, $size, $icon){
        if (empty($attachment_id)) return $image;
        if (
            (string) intval($attachment_id) != (string) $attachment_id && 
            $attachment_id != ''
        ) {
            $image[0]= $attachment_id;
        }
        return $image;
    }
    
    add_filter( 'wp_get_attachment_image_src', 'my_get_attachment_image_src', 10, 4);
    
  2. Create an additional meta box for posts and pages to show and set the URL based featured images.

    function add_my_meta_box() {
        add_meta_box(
            'post_thumbnail_url',
            __('External URL as featured Image'),
            'my_meta_box',
            'post',
            'side',
            'low'
        );
        add_meta_box(
            'page_thumbnail_url',
            __('External URL as featured Image'),
            'my_meta_box',
            'page',
            'side',
            'low'
        );
    }
    
    function my_meta_box($post) {
        $attachment_url = get_the_post_thumbnail_url($post->ID);
        if (
            (string) intval($attachment_url) != (string) $attachment_url && 
            $attachment_url != ''
        ) {
            echo "<img width="100%" id='my_thumbnail_preview' src="https://wordpress.stackexchange.com/questions/220574/{$attachment_url}">";
        }
        $placeholder = __('e.g. http://foo.bar/image.jpg');
        echo "<input type="text" placeholder="{$placeholder}" name="my_thumbnail_url" id='my_thumbnail_url' value="https://wordpress.stackexchange.com/questions/220574/{$attachment_url}">";
    }
    
    add_action( 'add_meta_boxes', 'add_my_meta_box');
    
  3. Add a save_post filter to handle the post meta _thumbnail_id (= Featured Image ID/URL).

    function save_my_thumbnail_url($post_id) {
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
            return;
        if ( !current_user_can( 'edit_post', $post_id ) )
            return;
        if ($_POST['my_thumbnail_url'] && !empty($_POST['my_thumbnail_url'])) {
            update_post_meta($post_id, '_thumbnail_id', $_POST['my_thumbnail_url']);
    }
        //TODO: Handle empty featured image url.
    }
    
    add_action( 'save_post', 'save_my_thumbnail_url', 99);
    

Now you can use the URL based featured image in your theme.

the_post_thumbnail();
// or…
$image_url = get_the_post_thumbnail_url(get_the_ID());
echo "<img src="https://wordpress.stackexchange.com/questions/220574/{$image_url}">"

Leave a Comment