Skip to content

Instantly share code, notes, and snippets.

@paperrobots
Last active September 22, 2016 19:43

Revisions

  1. paperrobots revised this gist Sep 22, 2016. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion functions.php
    Original file line number Diff line number Diff line change
    @@ -14,9 +14,13 @@ function fb_opengraph() {
    $img_src = get_stylesheet_directory_uri() . '/img/opengraph_image.jpg';
    }

    if($excerpt = $post->post_excerpt) {
    if($post->post_excerpt != '') {
    $excerpt = strip_tags($post->post_excerpt);
    $excerpt = str_replace("", "'", $excerpt);
    } elseif ($post->post_content != ''){
    $excerpt = strip_tags($post->post_content);
    $excerpt = str_replace("", "'", $excerpt);
    $excerpt = wp_trim_words($excerpt, 25);
    } else {
    $excerpt = get_bloginfo('description');
    }
  2. paperrobots revised this gist Sep 22, 2016. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,7 @@ function fb_opengraph() {
    } 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);
    @@ -21,10 +22,10 @@ function fb_opengraph() {
    }
    ?>

    <meta property="og:title" content="<?php echo the_title(); ?>"/>
    <meta property="og:title" content="<?php echo get_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:url" content="<?php echo get_permalink(); ?>"/>
    <meta property="og:site_name" content="<?php echo get_bloginfo(); ?>"/>
    <meta property="og:image" content="<?php echo $img_src; ?>"/>

  3. paperrobots created this gist Sep 22, 2016.
    33 changes: 33 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    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');

    function fb_opengraph() {
    global $post;

    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
    }
    add_action('wp_head', 'fb_opengraph', 5);