Removing sharedaddy’s (jetbacks sharing) extra og:description without hacking php
June 30th, 2012 | 2 comments
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:
|
721 722 723 724 725 726 727 728 729 |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
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(); } } } |
Short link: http://jkat.me/LXA7JD
Comments
Comment from >moray
Time: July 28, 2012, 5:25 am
just wondering where you put that hack? I have the same problem except Sharedaddy isnt picking up any description instead its using a ALL IN ONE SEO title as title and my blog title as a description
its driving me crazt
thanks
Comment from >Ken Judy
Time: July 28, 2012, 1:34 pm
I added it to my styles javascript file and then invoke it from the jQuery ready event handler (first testing to confirm jQuery is loaded – maybe excessive).
if (window.jQuery)
{
jQuery(document).ready(function() {
removeExtraOpenGraphMetaTags();
});
}