Skip to content

Instantly share code, notes, and snippets.

@mjangda
Created September 5, 2011 22:30
Show Gist options
  • Select an option

  • Save mjangda/1196068 to your computer and use it in GitHub Desktop.

Select an option

Save mjangda/1196068 to your computer and use it in GitHub Desktop.

Revisions

  1. mjangda revised this gist Oct 30, 2012. 1 changed file with 0 additions and 6 deletions.
    6 changes: 0 additions & 6 deletions wpcom-open-graph.php
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,4 @@
    <?php
    /*
    * WARNING: This file is distributed verbatim in Jetpack.
    * There should be nothing WordPress.com specific in this file.
    *
    * @hide-in-jetpack
    */
    /**
    * Open Graph Tags
    *
  2. mjangda revised this gist Oct 30, 2012. 1 changed file with 71 additions and 94 deletions.
    165 changes: 71 additions & 94 deletions wpcom-open-graph.php
    Original file line number Diff line number Diff line change
    @@ -1,147 +1,124 @@
    <?php
    /*
    * WARNING: This file is distributed verbatim in Jetpack.
    * There should be nothing WordPress.com specific in this file.
    *
    * @hide-in-jetpack
    */
    /**
    * Plugin Name: WP.com Open Graph Tags
    * Description: Add Open Graph tags so that Facebook (and any other service that supports them) can crawl the site better and we provide a better sharing experience
    * Author: Automattic
    * License: GPLv2, baby!
    * Open Graph Tags
    *
    * Add Open Graph tags so that Facebook (and any other service that supports them)
    * can crawl the site better and we provide a better sharing experience.
    *
    * @link http://ogp.me/
    * @link http://developers.facebook.com/docs/opengraph/
    */
    add_action( 'wp_head', 'jetpack_og_tags' );

    // Only enable for public and non-VIP blogs
    if( 1 == get_option( 'blog_public' ) && ( function_exists( 'wpcom_is_vip' ) && ! wpcom_is_vip() ) )
    add_action( 'wp_head', 'wpcom_og_tags' );
    function jetpack_og_tags() {
    if ( false === apply_filters( 'jetpack_enable_open_graph', true ) )
    return;

    function wpcom_og_tags() {
    $og_output = '';
    $tags = array();
    $image_width = 300;
    $image_height = 300;

    $image_width = 200;
    $image_height = 200;
    $description_length = 197;

    if ( is_home() || is_front_page() ) {
    $site_type = get_option( 'open_graph_protocol_site_type' );
    $tags['og:type'] = ! empty( $site_type ) ? $site_type : 'blog';

    $tags['og:title'] = get_bloginfo( 'name' );
    $tags['og:url'] = home_url();
    $tags['og:url'] = home_url( '/' );
    $tags['og:description'] = get_bloginfo( 'description' );

    // Associate a blog's root path with one or more Facebook accounts
    $facebook_admins = get_option( 'facebook_admins' );
    if ( ! empty( $facebook_admins ) )
    $tags['fb:admins'] = $facebook_admins;

    } elseif ( is_author() ) {
    $tags['og:type'] = 'author';

    $author = get_queried_object();

    $tags['og:title'] = $author->display_name;
    $tags['og:url'] = get_author_posts_url( $author->ID );
    $tags['og:description'] = $author->description;

    } elseif ( is_singular() ) {
    $tags['og:type'] = 'article';
    global $post;
    setup_postdata( $post );

    $tags['og:type'] = 'article';
    $tags['og:title'] = get_the_title();
    $tags['og:url'] = get_permalink();

    $tags['og:description'] = strip_tags( get_the_excerpt() );
    }

    if ( empty( $tags ) )
    return;

    $tags['og:site_name'] = get_bloginfo( 'name' );

    $tags['og:image'] = wpcom_og_get_image( $image_width, $image_height );

    $tags['og:image'] = jetpack_og_get_image( $image_width, $image_height );
    if ( is_array( $tags['og:image'] ) ) {
    // We got back more than one image from Jetpack_PostImages, so we need to extract just the thumb
    $imgs = array();
    foreach ( $tags['og:image'] as $img ) {
    $imgs[] = jetpack_photon_url( $img['src'], array( 'fit' => '400,400' ) );
    }
    $tags['og:image'] = $imgs;
    }

    // Facebook whines if you give it an empty title
    if( empty( $tags['og:title'] ) )
    if ( empty( $tags['og:title'] ) )
    $tags['og:title'] = __( '(no title)' );

    // Shorten the description if it's too long
    $tags['og:description'] = strlen( $tags['og:description'] ) > $description_length ? substr( $tags['og:description'], 0, $description_length ) . '...' : $tags['og:description'];
    $tags['og:description'] = strlen( $tags['og:description'] ) > $description_length ? mb_substr( $tags['og:description'], 0, $description_length ) . '...' : $tags['og:description'];

    // Add any additional tags here, or modify what we've come up with
    $tags = apply_filters( 'jetpack_open_graph_tags', $tags );

    $tags = apply_filters( 'wpcom_open_graph_tags', $tags );

    foreach ( (array) $tags as $tag_property => $tag_content ) {
    foreach( (array) $tag_content as $tag_content_single ) { // to accomodate multiple images
    $og_output .= sprintf( '<meta property="%s" content="%s" />', esc_attr( $tag_property ), esc_attr( $tag_content_single ) );
    foreach ( (array) $tag_content as $tag_content_single ) { // to accomodate multiple images
    if ( empty( $tag_content_single ) )
    continue; // Don't ever output empty tags
    $og_tag = sprintf( '<meta property="%s" content="%s" />', esc_attr( $tag_property ), esc_attr( $tag_content_single ) );
    $og_output .= apply_filters( 'jetpack_open_graph_output', $og_tag );
    $og_output .= "\n";
    }
    }
    echo $og_output;

    echo $og_output;
    }

    function wpcom_og_get_image( $width = 50, $height = 50, $max_images = 4 ) { // Facebook requires thumbnails to be a minimum of 50x50
    function jetpack_og_get_image( $width = 50, $height = 50, $max_images = 4 ) { // Facebook requires thumbnails to be a minimum of 50x50
    $image = '';
    if ( is_singular() ) {

    if ( is_singular() && !is_home() && !is_front_page() ) {
    global $post;

    $images = array();
    $size = array( $width, $height );
    $size_query_args = array( 'w' => $width, 'h' => $height );
    $content = $post->post_content;

    // If a featured image is set, use that
    if( function_exists( 'has_post_thumbnail' ) && has_post_thumbnail() ) {
    $image_src = wp_get_attachment_image_src( get_post_thumbnail_id(), $size );
    if( ! empty( $image_src ) )
    $images[] = add_query_arg( $size_query_args, $image_src[0] );
    }

    // If the post has a gallery, use the images from that gallery
    if( preg_match( '/\[gallery[^\]]*\]/', $content ) ) {
    $count = $max_images - count( $images );
    $gallery_images = get_posts( array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'posts_per_page' => $count,
    'post_parent' => $post->ID,
    ) );

    if( $gallery_images ) {
    foreach ( $gallery_images as $gallery_image ) {
    $image_src = wp_get_attachment_image_src( $gallery_image->ID, $size );
    if( ! empty( $image_src ) )
    $images[] = add_query_arg( $size_query_args, $image_src[0] );
    }
    }

    }

    // Fallback to the other images in the post
    $offset = 0;
    while( count( $images ) < $max_images ) {
    if( preg_match( '/<img[^>]*src=[\'"](.*?)[\'"][^>]*>/i', $content, $match, PREG_OFFSET_CAPTURE, $offset ) ) {
    $images[] = add_query_arg( $size_query_args, $match[1][0] );
    $offset = $match[1][1];
    } else {
    break;
    }
    }

    // Get rid of any duplicates
    $image = array_unique( $images );

    } elseif( is_author() ) {
    $image = '';

    // Attempt to find something good for this post using our generalized PostImages code
    if ( class_exists( 'Jetpack_PostImages' ) )
    $image = Jetpack_PostImages::get_images( $post->ID, array( 'width' => $width, 'height' => $height ) );
    } else if ( is_author() ) {
    $author = get_queried_object();
    $avatar = get_avatar_url( $author->user_email, $width );
    if( ! empty( $avatar ) )

    if ( ! empty( $avatar ) )
    $image = $avatar[0];
    }

    // Fallback to Blavatar -- the iOS one is nice as a default
    $blavatar_domain = blavatar_domain( site_url() );
    if( empty( $image ) && blavatar_exists( $blavatar_domain ) )
    $image = blavatar_url( $blavatar_domain, 'img', $width );


    // Fallback to Blavatar if available
    if ( function_exists( 'blavatar_domain' ) ) {
    $blavatar_domain = blavatar_domain( site_url() );
    if ( empty( $image ) && blavatar_exists( $blavatar_domain ) )
    $image = blavatar_url( $blavatar_domain, 'img', $width );
    }

    return $image;
    }
  3. mjangda revised this gist Apr 16, 2012. 1 changed file with 17 additions and 19 deletions.
    36 changes: 17 additions & 19 deletions wpcom-open-graph.php
    Original file line number Diff line number Diff line change
    @@ -9,19 +9,17 @@
    * @link http://developers.facebook.com/docs/opengraph/
    */

    if ( ! function_exists( 'wpcom_og_tags' ) ) :

    // Only enable for public blogs
    if( 1 == get_option( 'blog_public' ) )
    // Only enable for public and non-VIP blogs
    if( 1 == get_option( 'blog_public' ) && ( function_exists( 'wpcom_is_vip' ) && ! wpcom_is_vip() ) )
    add_action( 'wp_head', 'wpcom_og_tags' );

    function wpcom_og_tags() {
    $og_output = '';
    $tags = array();

    $image_width = apply_filters( 'wpcom_open_graph_image_width', 300 );
    $image_height = apply_filters( 'wpcom_open_graph_image_height', 300 );
    $description_length = apply_filters( 'wpcom_open_graph_description_length', 197 );
    $image_width = 300;
    $image_height = 300;
    $description_length = 197;

    if ( is_home() || is_front_page() ) {
    $site_type = get_option( 'open_graph_protocol_site_type' );
    @@ -50,9 +48,8 @@ function wpcom_og_tags() {

    $tags['og:title'] = get_the_title();
    $tags['og:url'] = get_permalink();

    // We want to avoid filters that add things like Sharing buttons, so let's scrub it ourselves
    $tags['og:description'] = trim( strip_tags( strip_shortcodes( get_the_content() ) ) );

    $tags['og:description'] = strip_tags( get_the_excerpt() );
    }

    if ( empty( $tags ) )
    @@ -134,16 +131,17 @@ function wpcom_og_get_image( $width = 50, $height = 50, $max_images = 4 ) { // F
    $image = array_unique( $images );

    } elseif( is_author() ) {
    if( function_exists( 'get_avatar_url' ) ) {
    $author = get_queried_object();
    $avatar = get_avatar_url( $author->user_email, $width );

    if( ! empty( $avatar ) )
    $image = $avatar[0];
    }
    $author = get_queried_object();
    $avatar = get_avatar_url( $author->user_email, $width );

    if( ! empty( $avatar ) )
    $image = $avatar[0];
    }

    // Fallback to Blavatar -- the iOS one is nice as a default
    $blavatar_domain = blavatar_domain( site_url() );
    if( empty( $image ) && blavatar_exists( $blavatar_domain ) )
    $image = blavatar_url( $blavatar_domain, 'img', $width );

    return $image;
    }

    endif;
  4. mjangda revised this gist Dec 14, 2011. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions wpcom-open-graph.php
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,9 @@
    /**
    * Plugin Name: WP.com Open Graph Tags
    * Description: Add Open Graph tags so that Facebook (and any other service that supports them) can crawl the site better and we provide a better sharing experience
    * Author: Automattic
    * License: GPLv2, baby!
    *
    * @link http://ogp.me/
    * @link http://developers.facebook.com/docs/opengraph/
    */
  5. mjangda revised this gist Nov 24, 2011. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion wpcom-open-graph.php
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,13 @@
    <?php

    /**
    * Plugin Name: WP.com Open Graph Tags
    * Description: Add Open Graph tags so that Facebook (and any other service that supports them) can crawl the site better and we provide a better sharing experience
    * @link http://ogp.me/
    * @link http://developers.facebook.com/docs/opengraph/
    */

    if ( ! function_exists( 'wpcom_og_tags' ) ) :

    // Only enable for public blogs
    if( 1 == get_option( 'blog_public' ) )
    add_action( 'wp_head', 'wpcom_og_tags' );
    @@ -141,3 +142,5 @@ function wpcom_og_get_image( $width = 50, $height = 50, $max_images = 4 ) { // F

    return $image;
    }

    endif;
  6. mjangda revised this gist Oct 12, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wpcom-open-graph.php
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ function wpcom_og_tags() {
    $image_height = apply_filters( 'wpcom_open_graph_image_height', 300 );
    $description_length = apply_filters( 'wpcom_open_graph_description_length', 197 );

    if ( is_home() ) {
    if ( is_home() || is_front_page() ) {
    $site_type = get_option( 'open_graph_protocol_site_type' );
    $tags['og:type'] = ! empty( $site_type ) ? $site_type : 'blog';

  7. mjangda created this gist Sep 5, 2011.
    143 changes: 143 additions & 0 deletions wpcom-open-graph.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,143 @@
    <?php

    /**
    * Plugin Name: WP.com Open Graph Tags
    * Description: Add Open Graph tags so that Facebook (and any other service that supports them) can crawl the site better and we provide a better sharing experience
    * @link http://ogp.me/
    * @link http://developers.facebook.com/docs/opengraph/
    */

    // Only enable for public blogs
    if( 1 == get_option( 'blog_public' ) )
    add_action( 'wp_head', 'wpcom_og_tags' );

    function wpcom_og_tags() {
    $og_output = '';
    $tags = array();

    $image_width = apply_filters( 'wpcom_open_graph_image_width', 300 );
    $image_height = apply_filters( 'wpcom_open_graph_image_height', 300 );
    $description_length = apply_filters( 'wpcom_open_graph_description_length', 197 );

    if ( is_home() ) {
    $site_type = get_option( 'open_graph_protocol_site_type' );
    $tags['og:type'] = ! empty( $site_type ) ? $site_type : 'blog';

    $tags['og:title'] = get_bloginfo( 'name' );
    $tags['og:url'] = home_url();
    $tags['og:description'] = get_bloginfo( 'description' );

    // Associate a blog's root path with one or more Facebook accounts
    $facebook_admins = get_option( 'facebook_admins' );
    if ( ! empty( $facebook_admins ) )
    $tags['fb:admins'] = $facebook_admins;

    } elseif ( is_author() ) {
    $tags['og:type'] = 'author';

    $author = get_queried_object();

    $tags['og:title'] = $author->display_name;
    $tags['og:url'] = get_author_posts_url( $author->ID );
    $tags['og:description'] = $author->description;

    } elseif ( is_singular() ) {
    $tags['og:type'] = 'article';

    $tags['og:title'] = get_the_title();
    $tags['og:url'] = get_permalink();

    // We want to avoid filters that add things like Sharing buttons, so let's scrub it ourselves
    $tags['og:description'] = trim( strip_tags( strip_shortcodes( get_the_content() ) ) );
    }

    if ( empty( $tags ) )
    return;

    $tags['og:site_name'] = get_bloginfo( 'name' );

    $tags['og:image'] = wpcom_og_get_image( $image_width, $image_height );

    // Facebook whines if you give it an empty title
    if( empty( $tags['og:title'] ) )
    $tags['og:title'] = __( '(no title)' );

    // Shorten the description if it's too long
    $tags['og:description'] = strlen( $tags['og:description'] ) > $description_length ? substr( $tags['og:description'], 0, $description_length ) . '...' : $tags['og:description'];

    $tags = apply_filters( 'wpcom_open_graph_tags', $tags );

    foreach ( (array) $tags as $tag_property => $tag_content ) {
    foreach( (array) $tag_content as $tag_content_single ) { // to accomodate multiple images
    $og_output .= sprintf( '<meta property="%s" content="%s" />', esc_attr( $tag_property ), esc_attr( $tag_content_single ) );
    $og_output .= "\n";
    }
    }

    echo $og_output;
    }

    function wpcom_og_get_image( $width = 50, $height = 50, $max_images = 4 ) { // Facebook requires thumbnails to be a minimum of 50x50
    $image = '';

    if ( is_singular() ) {
    global $post;

    $images = array();
    $size = array( $width, $height );
    $size_query_args = array( 'w' => $width, 'h' => $height );
    $content = $post->post_content;

    // If a featured image is set, use that
    if( function_exists( 'has_post_thumbnail' ) && has_post_thumbnail() ) {
    $image_src = wp_get_attachment_image_src( get_post_thumbnail_id(), $size );
    if( ! empty( $image_src ) )
    $images[] = add_query_arg( $size_query_args, $image_src[0] );
    }

    // If the post has a gallery, use the images from that gallery
    if( preg_match( '/\[gallery[^\]]*\]/', $content ) ) {
    $count = $max_images - count( $images );
    $gallery_images = get_posts( array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'posts_per_page' => $count,
    'post_parent' => $post->ID,
    ) );

    if( $gallery_images ) {
    foreach ( $gallery_images as $gallery_image ) {
    $image_src = wp_get_attachment_image_src( $gallery_image->ID, $size );
    if( ! empty( $image_src ) )
    $images[] = add_query_arg( $size_query_args, $image_src[0] );
    }
    }

    }

    // Fallback to the other images in the post
    $offset = 0;
    while( count( $images ) < $max_images ) {
    if( preg_match( '/<img[^>]*src=[\'"](.*?)[\'"][^>]*>/i', $content, $match, PREG_OFFSET_CAPTURE, $offset ) ) {
    $images[] = add_query_arg( $size_query_args, $match[1][0] );
    $offset = $match[1][1];
    } else {
    break;
    }
    }

    // Get rid of any duplicates
    $image = array_unique( $images );

    } elseif( is_author() ) {
    if( function_exists( 'get_avatar_url' ) ) {
    $author = get_queried_object();
    $avatar = get_avatar_url( $author->user_email, $width );

    if( ! empty( $avatar ) )
    $image = $avatar[0];
    }
    }

    return $image;
    }