How can I control the Facebook like image?

I have a blog with some posts, and each post has a embedded Facebook like button. Pressing the button opens a dialog so my visitors can share the post on Facebook with a comment.

When sharing, however, the image selected by Facebook is a generic mail icon and not the post thumbnail.

Read More

How can I control the image that is used when sharing?

Related posts

Leave a Reply

7 comments

  1. The image that is used for sharing is taken from a chunk of code in the header of your site that will look something like this:

    <link rel="image_src" href="path/to/theme/screenshot.png" />
    

    Typically it links to the screenshot of your site in the theme. If you removed the code from the header of the file and on single.php put it inside the loop and called your post thumbnail image into the href element I believe it would work. So it would look something like:

    <link rel="image_src" href="<?php the_post_thumbnail(); ?>" />
    

    This would mean that if you have like buttons on pages that list multiple posts you probably won’t have an image. If you included some conditional code that removed it only on single.php then you would have the normal image on any page with multiple posts and an like button and the post thumbnail when the single.php template is being used. So the header code would be:

    <?php if ( is_single() ) { /* do nothing on single pages */ } else { ?>
    <link rel="image_src" href="path/to/theme/screenshot.png" />
    <?php } ?>
    

    Then you’d still use the code to include the post thumbnail in single.php.

  2. You need to use the Facebook Open Graph Protocol. I’m not sure why the Accepted answer isn’t one of the many OG: related answers (which I’ve voted up) but it is wrong.

    http://developers.facebook.com/docs/opengraph/

    You can customize many things including title, image, description, category, last update etc… if you use Open Graph. If you use these other half-solutions you are missing the whole picture.

    If I didn’t follow OG Protocol for all the FB work I do, I’d get fired.

  3. If you do the following:

    <link rel="image_src" href="<?php the_post_thumbnail(); ?>" />
    

    You’ll find that WordPress outputs the the html required to display the image, not just the SRC which is what you really want.

    Doing something like:

    <?php
    
    // Featured Image for FB Like
    $feature_image = get_the_post_thumbnail($post->ID);
    
    // Get image source
    $doc = new DOMDocument();
    $doc->loadHTML($feature_image);
    $imageTags = $doc->getElementsByTagName('img');
    
    foreach($imageTags as $tag) {
            $image_url = $tag->getAttribute('src');
        }
    ?>
    <link rel="image_src" href="<?php echo $image_url; ?>" />
    

    gets you the URL only. It’s probably the long way and can probably but shortened, but it definitely fixes the problem.

    Hope this puts you on the right track.

    Matt.

  4. <meta property="og:image" content="http://YOUR_IMAGE_URL" />
    

    This method works for “like”:s but later on if you want to share a link in ur log(For example.) this picture is autoselected.

    Without this meta tag you are able to select from all the images on the linked site.

    Anyone know any way to keep the static “like” image but still make you choose an image when sharing a url?