Skip to content

Instantly share code, notes, and snippets.

@natebot
Last active November 22, 2021 18:24
Show Gist options
  • Save natebot/6323846 to your computer and use it in GitHub Desktop.
Save natebot/6323846 to your computer and use it in GitHub Desktop.
Filter jetpack open graph tags for fixes to og:meta and twitter cards, and adding support for Facebook's article:publisher tag for the WordPress VIP environment. Please note that you need to replace YOUR_TWITTER_HANDLE and YOUR_FACEBOOK_PAGE with appropriate values.
<?php
/* ------------------------------------------------------------------------------
* FILTER JETPACK OPEN GRAPH TAGS ADDITIONAL TWITTER CARD SUPPORT, FACEBOOK PUBLISHER, AND OG:META FIXES
* For the WordPress VIP environment.
* @see https://dev.twitter.com/docs/cards
* @see https://developers.facebook.com/blog/post/2013/06/19/platform-updates--new-open-graph-tags-for-media-publishers-and-more/
* ---------------------------------------------------------------------------- */
add_filter( 'jetpack_open_graph_tags', function( $tags ){
// TWITTER FIXES:
// wpcom defaults to the 'photo' card type for all posts but it should be 'summary' or not used at all ('summary' is default if no card type is specified.)
if( is_single() && $tags['twitter:card' ] === 'photo' )
unset( $tags['twitter:card' ] );
// wpcom sets card author to @wordpresscom, you should set it to your site's twitter handle ( or the author's handle. )
if( is_single() )
$tags['twitter:creator']= sprintf( '@%s', YOUR_TWITTER_HANDLE );
// OG META FIXES
// Jetpack doesn't provide a description for the homepage so use the blog's description
// Jetpack uses 'article' for the site's home and 'website' is more appropriate.
if( is_home() ){
$tags['og:description'] = esc_attr( get_bloginfo( 'description' ) );
$tags['og:type'] = 'website';
}
// FACEBOOK ARTICLE PUBLISHER TAG SUPPORT
if( is_single() )
$tags['article:publisher'] = 'https://www.facebook.com/YOUR_FACEBOOK_PAGE';
// return the modified open graph tags
return $tags;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment