I’ve come across a problem where multiple plugins insert facebook opengraph meta tags into the header of my posts without letting me turn that behavior on or off. They all assume that no one explicitly puts open graph tags in their blog. This includes sharedaddy (jetpack sharing). It does so with the following comment:
public function display_header() { if ( $this->share_type == 'share' ) { // Set the open graph description, otherwise Facebook may pick up some random text from the page global $post; if ( $post && $post->ID > 0 ) echo '<meta property="og:description" content="'.esc_attr( apply_filters( 'sharing_post_title', $post->post_title, $post->ID, $this->id ) ).'" />'; } }
Problem is I’d rather define my og tags or rely on a plugin focused on doing this well.
There are several plugins to do open graph tag insertion that do a better job. You may want to roll your own directly into your theme or using one of the general SEO plugins.
So, I want to remove these extraneous tags. Online tips suggest I go into each plugin and comment out the php code inserting these tags. That’s a direct option but I hate modifying plugins. I don’t want to maintain these hacks through updates.
So, I solved with javascript. The approach is a hack. It requires that sharedaddy and other plugins insert their og:description tags before the tags I want to preserve and that I start my desired facebook tags with an fb:admins tag.
function removeExtraOpenGraphMetaTags() { //assumes extra og tags occur before an fb:admins var first_og_tag = jQuery("meta[property]").first(); //if the first meta tag with a property attribute is fb:admins - do nothing if (jQuery("meta[property]").first().attr('property') != 'fb:admins') { //remove all subsequent og tags until fb:admins var removed_tags = first_og_tag.nextUntil("meta[property='fb:admins']", "meta[property*='og:']").remove(); if (removed_tags) { first_og_tag.remove(); } } }