1

if i try to publich my website on Facebook i have an error with the featured image.

I try to use the facebook debug but i not have any interest information.

I use this function but nothing resolve:

//* FACEBOOK *//

function insert_fb_in_head() {
    global $post;
    if ( !is_singular()) // Se non è un post o una pagina
        return;

        $mytext=$post->post_excerpt;
        $myfulltext=strip_tags($post->post_content);
        if(strlen($mytext) > 250) $mytext = substr($mytext, 0, 250).'...';
        if(strlen($myfulltext) > 250) $myfulltext = substr($myfulltext, 0, 250).'...';
        if(empty($post->post_excerpt)) {
        $mytext=$myfulltext;
        }

        $mydesc=$mytext;
    $external_posts=$post->ID;

        echo '<meta property="fb:admins" content="IDPAGE" />';
        echo "\n";
        echo '<meta property="og:title" content="' . get_the_title() . '" />';
        echo "\n";
        echo '<meta property="og:type" content="article" />';
        echo "\n";
        echo '<meta property="og:description" content="' . $mydesc . '" />';
        echo "\n";
        echo '<meta property="og:url" content="' . get_permalink() . '" />';
        echo "\n";
        echo '<meta property="og:site_name" content="TITLE WEBSITE" />';
        echo "\n";
        echo '<meta property="og:locale" content="it_IT" />';
        echo "\n";

        $myurl = wp_get_attachment_image_src( get_post_thumbnail_id($external_posts), 'thumbnail' );
        $postimage=$myurl[0];

        if(empty($postimage)) {
        $default_image="URLIMAGE";
        echo '<meta property="og:image" content="' . $default_image . '" />';
        }
        else {
        $default_image=$postimage;
        echo '<meta property="og:image" content="' . $default_image . '" />';
        }
        echo "\n"; echo "\n";

}

add_action( 'wp_head', 'insert_fb_in_head', 1 );

So i don't know how can i do to set a specific image. Thanks

1 Answer 1

0

Try this: In your functions.php put this

function fb_excerpt($text, $excerpt) {
    if ($excerpt) return $excerpt;

    $raw_excerpt = $text;

    $text = strip_shortcodes( $text );

    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]&gt;', $text);
    $text = strip_tags($text);
    $excerpt_length = apply_filters('excerpt_length', 55);
    $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
    if (count($words) > $excerpt_length) {
            array_pop($words);
            $text = implode(' ', $words);
            $text = $text . '...';
    } else { $text = implode(' ', $words); }

    return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}

and in your header.php between you <head> and </head> tags put this

<?php global $post;
if (is_singular()) { ?>
    <meta property="fb:admins" content="522774907844630">
    <meta property="og:title" content="<?php the_title(); ?>">
    <meta property="og:description" content="<?php echo fb_excerpt($post->post_content, get_the_excerpt()); ?>">
    <meta property="og:type" content="article">
    <meta property="og:image" content="<?php echo wp_get_attachment_url(get_post_thumbnail_id($post->ID)); ?>">
    <meta property="og:site_name" content="<?php bloginfo('name'); ?>">
    <meta property="og:url" content="<?php echo('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>">
<?php } ?>
2
  • 3
    This code will fail without a global $post;, you should consider using the wp_head hook instead to implement functionality in the header ( like the OP did in their question )
    – Tom J Nowell
    Mar 25, 2015 at 0:13
  • Oh. I wondered why would it fail because I dont have global $post anywhere but again, I have fallen for the bones dilemma. I think I should develop on vanilla WP for a while before I keep answering here. Hah. Thanks for the input mate. Altough it would be fairly trivial to transfer this to the wp_head hook as well.
    – jimihenrik
    Mar 25, 2015 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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