Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to add the featured image URL of a post to the header. When a Facebook user shares a Wordpress page, this code in the header:

The rel="image_src" attribute is what facebook is searching for.

<link rel="image_src" href="FEATUREDIMAGEURL">

Will return a specific image for the share. However, I cannot figure out how to add the URL of the post's featured image... Can you?

I tried this:

<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ); ?>
<link rel="image_src" href="<?php echo $image; ?>">
<?php endif; ?>

But it gave me a Parse error: syntax error, unexpected ';' error.

share|improve this question
    
You're missing a bracket after get_post_thumbnail_id();. So it's exactly what error message tells you. Hint: There's a line number and filename hidden in the error message. –  kaiser Oct 23 '12 at 16:37

2 Answers 2

up vote 4 down vote accepted

To answer this one and point to the real problem:

As the <head> HTML tag comes far before the actual loop, you'll need something else than the global $post.

get_queried_object();
get_queried_object_id();

The plugin

The code is tested and works.

As you might want to keep this functionality when switching themes, I'd suggest wrapping it up in a plugin.

So the actual plugin should be something around the following lines:

<?php
/** Plugin Name: (#70215) »kaiser« Post Thumbnail image for FB */
function wpse70215_fb_img()
{
    // Not on a single page or post? Stop here.
    if ( ! is_singular() )
        return;

    $post_ID = get_queried_object_id();

    // We got no thumbnail? Stop here.
    if ( ! has_post_thumbnail( $post_ID ) )
        return;

    // Get the Attachment ID
    $att_ID = get_post_thumbnail_id( $post_ID );

    // Get the Attachment
    $att    = wp_get_attachment_image_src( $att_ID );

    printf(
         '<link rel="image_src" href="%s">'
        ,array_shift( $att )
    );
}
add_action( 'wp_head', 'wpse70215_fb_img' );
share|improve this answer
    
@AmandaDuke Could you please clean up your comments? They don't add value any more. Thanks. –  kaiser Oct 23 '12 at 17:55
    
I believe that there is a form of bug in this code. I added an array here wp_get_attachment_image_src( $att_ID, array ( 578 ) ); This should prioritise images with width 578px, correct? Apparently, it seems to work sometimes but sometimes it selects a different size even though 578px version exists. Do you know why? Also, I selected this answer as correct as I ended up using this solution than the other one posted. –  Amanda Duke Dec 7 '12 at 19:40
    
@AmandaDuke 578px is some size. You decided to add that. What that answer shows is just a core solution... there's nothing that's decide to take a specific amount of pixels for an img. –  kaiser Dec 8 '12 at 2:16

there was a closing bracket missing in the second line; and you need to reference the first array element of $image:

<?php if (has_post_thumbnail( $post->ID ) ): ?> 
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ) ); ?> 
<link rel="image_src" href="<?php echo $image[0]; ?>"> 
<?php endif; ?> 

http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

you might also want to add a check for single post or page, to avoid any unwanted output in index or archive pages; example:

<?php if ( has_post_thumbnail( $post->ID ) && is_singular() ): ?>
share|improve this answer
    
Thank you...... –  Amanda Duke Oct 23 '12 at 16:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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