WordPress – how to insert code into the head?

I’m having a Like button on my WordPress site. I’d like to use the og:image meta-tag from facebook to display the proper image when somebody likes it.
I need to add that tag in the head of the page.
So far I have this in single.php:

function fb_image_meta($image) {
    return '<meta property="og:image" content="$image" />';
}

add_action('wp_head', 'fb_image_meta', 10, $image_thumbnail); 

When I look at the source of an article, The meta tag doesn’t show up.

Read More

What am I doing wrong?

Related posts

Leave a Reply

2 comments

  1. Use this code in functions.php

    function fb_image_meta($image) {
        echo '<meta property="og:image" content="$image" />';
    }
    
    add_action('wp_head', 'fb_image_meta', 10, $image_thumbnail);
    

    You should take care of $image_thumbnail – it must be declared before wp_head. If this is a post thumbnail, you can get it with this code (before the add_action call):

    global $post;
    $image_thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
    
  2. Based off of Nikolay’s answer:

    function fb_image_meta($image) {
        echo '<meta property="og:image" content="$image" />';
    }
    
    add_action('wp_head', 'fb_image_meta', 10, 1);
    

    Find and replace your wp_head() call (probably in header.php) with this:

    do_action('wp_head', $fbImage);