1

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

  • This can be done but at least you have to save the URL of image somewhere, may be post meta! – Sumit Mar 13 '16 at 18:54
  • please write more details, inserting post in default way or by front-end form ? if u used your form so you can save url of image as post meta like @Sumit said – Kika Mar 13 '16 at 19:09
  • @Sumit I use imageshack, or maybe I don't understand what you mean??? – Aurora A Mar 13 '16 at 20:52
  • Please check the @iantsch answer, I meant the same. – Sumit Mar 14 '16 at 4:46
5

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='{$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='{$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='{$image_url}'>"
| improve this answer | |
  • @Sumit i edited my answer, please remove down vote if you think this might answer the question accordingly – iantsch Mar 13 '16 at 20:17
  • A sidenote iantsch, your original answer was just a link, your answer needs to contain the answer, links die, bitrot sets in, and your answer needs to stand on its own. External links can support it, but they can't be the answer itself. – Tom J Nowell Mar 14 '16 at 1:05
  • Also, this answer doesn't download the image, so I don't think @Sumit 's comment is accurate here as it currently stands. The answer could be improved with some information explaining how a featured thumbnail is stored as an attachment ID in post meta, as well as some basics on how media in WP is represented – Tom J Nowell Mar 14 '16 at 1:06
  • 1
    @TomJNowell i improved the answer with some more information. – iantsch Mar 14 '16 at 2:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.