-
-
Save alexkingorg/3956212 to your computer and use it in GitHub Desktop.
Add Open Graph tags to your WordPress site
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: AK.org Strict 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 and Alex King | |
* License: GPLv2, baby! | |
* | |
* @link http://ogp.me/ | |
* @link http://developers.facebook.com/docs/opengraph/ | |
*/ | |
add_action( 'wp_head', 'wpcom_og_tags' ); | |
function wpcom_og_tags() { | |
$og_output = ''; | |
$tags = array(); | |
$image_width = 300; | |
$image_height = 300; | |
$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: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; | |
} | |
else if ( 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; | |
} | |
else if ( is_singular() ) { | |
$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(); | |
// 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() { // Facebook requires thumbnails to be a minimum of 50x50 | |
$image = ''; | |
if ( is_singular() ) { | |
$size = apply_filters('ak_og_img_size', 'medium'); | |
// 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 ); | |
$image = $image_src[0]; | |
} | |
} | |
else if ( is_author() ) { | |
$author = get_queried_object(); | |
$avatar = get_avatar_url( $author->user_email, 100 ); | |
if ( ! empty( $avatar ) ) | |
$image = $avatar[0]; | |
} | |
// Fallback to default | |
if ( empty( $image ) ) { | |
$url = apply_filters('ak_og_default_img', trailingslashit(get_stylesheet_directory_uri()).'og-image.png'); | |
$image = $url; | |
} | |
return $image; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment