• Resolved miketurco

    (@miketurco)


    The Yoast plugin doesn’t work (for me) for facebook. When I post a link, for example, no picture, etc. I’m proposing this as a temporary fix for those who face the same issue.

    The “description” field auto fills from the post excerpt field. The image comes from the Featured Image field. (Some themes don’t support featured image. If this is the case, you’ll have to enable that in functions.php. The solution is easy to find online.)

    Out of a bit of frustration, here’s a plugin I put together. (I copied/pasted from various threads to put this together. Not my original work.)

    I unchecked the box that says “Add Open Graph meta data” on the Facebook tab of the Social settings.

    The problem I’m having with OG doesn’t seem to be related to settings. But… could be my fault. In any event, if someone has a better way to do this I’d appreciate hearing about it. Especially if it’s something that will allow me to use the Yoast plugin itself to handle OG.

    There’s no download for this. You’ll have to copy/paste this code to a file called fb-og.php and drop that file in your plugins folder.

    Mike

    <?php
    /*
    Plugin Name: FB OG
    Description: Adds open graph info for facebook
    Version: 1.0
    Author: Mike Turco
    Author URI: (elided)
    License: GPL2
    */
    
    function fb_opengraph() {
        global $post;
    
        if(is_single()) {
            if(has_post_thumbnail($post->ID)) {
             //   $img_src = wp_get_attachment_image_src(wp_get_attachment_url( get_post_thumbnail_id($post->ID) ));
    $thumb_id = get_post_thumbnail_id();
    $thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
    $thumb_url = $thumb_url_array[0];
    
            } else {
                $img_src = get_stylesheet_directory_uri() . '/img/opengraph_image.jpg';
            }
            if($excerpt = $post->post_excerpt) {
                $excerpt = strip_tags($post->post_excerpt);
                $excerpt = str_replace("", "'", $excerpt);
            } else {
                $excerpt = get_bloginfo('description');
            }
            ?>
    
        <meta property="og:title" content="<?php echo the_title(); ?>"/>
        <meta property="og:description" content="<?php echo $excerpt; ?>"/>
        <meta property="og:type" content="article"/>
        <meta property="og:url" content="<?php echo the_permalink(); ?>"/>
        <meta property="og:site_name" content="<?php echo get_bloginfo(); ?>"/>
        <meta property="og:image" content="<?php echo $thumb_url; ?>"/>
    
    <?php
        } else {
            return;
        }
    }
    add_action('wp_head', 'fb_opengraph', 5);
    
    ?>

    https://www.ads-software.com/plugins/wordpress-seo/

  • The topic ‘Fix for FB OG problem’ is closed to new replies.