Last active
June 7, 2018 14:41
-
-
Save DevWael/1b6b6bd1d55f3dcdc7be02811b4b2225 to your computer and use it in GitHub Desktop.
Adding Open Graph Meta Data in WordPress Using Official Facebook Plugin
This file contains 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 | |
add_filter('language_attributes', 'aprefix_add_opengraph_doctype'); | |
function aprefix_add_opengraph_doctype( $output ) { | |
return $output . ' xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml"'; | |
} | |
add_action( 'wp_head', 'prefix_insert_fb_in_head', 5 ); | |
function prefix_insert_fb_in_head() { | |
global $post; | |
if ( !is_singular()) //if it is not a post or a page | |
return; | |
echo '<meta property="fb:admins" content="YOUR USER ID"/>'; | |
echo '<meta property="og:title" content="' . get_the_title() . '"/>'; | |
echo '<meta property="og:type" content="article"/>'; | |
echo '<meta property="og:description" content="'.get_the_excerpt().'"/>'; | |
echo '<meta property="og:url" content="' . get_permalink() . '"/>'; | |
echo '<meta property="og:site_name" content="Your Site NAME Goes HERE"/>'; | |
if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image | |
$default_image="http://example.com/image.jpg"; //replace this with a default image on your server or an image in your media library | |
echo '<meta property="og:image" content="' . $default_image . '"/>'; | |
} else { | |
$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' ); | |
echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>'; | |
} | |
echo " | |
"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment