• Hey all! I know that there are plugins for this. However, is there code that we can do instead to change this information. I want to change what facebook and other social media show when I link to a site.

    Thanks in advance.

Viewing 1 replies (of 1 total)
  • It will be more simple with yoast plugin but if you wish to code you can add next line of code in your functions.php file of your theme to see what will do.

    1. This function lets Open Graph parsers read your meta tags properly. You can use the language_attributes filter to add this right to your html tag.

    function doctype_opengraph($output) {
        return $output . '
        xmlns:og="http://opengraphprotocol.org/schema/"
        xmlns:fb="http://www.facebook.com/2008/fbml"';
    }
    add_filter('language_attributes', 'doctype_opengraph');

    2. The next function is where you will actually add the proper metadata for Open Graph to work.

    function fb_opengraph() {
        global $post;
    
        if(is_single()) {
            if(has_post_thumbnail($post->ID)) {
                $img_src = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium');
            } 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 $img_src; ?>"/>
    
    <?php
        } else {
            return;
        }
    }
    add_action('wp_head', 'fb_opengraph', 5);

Viewing 1 replies (of 1 total)

The topic ‘OG: URL, Title, Image’ is closed to new replies.