
WordPress plugin and theme developers may sometimes find the need to output your own open-graph tags, meta descriptions or titles, for specific pages. But one consideration is if your user is running a SEO plugin, such as the Yoast WordPress SEO plugin or the All-In-One SEO Pack. You don't want your plugin or theme to output a second set of open-graph or meta tags, so being able to disable those plugins as needed, within your code, would be a plus.
Disabling the Yoast SEO and All-In-One SEO plugins (the two most common SEO plugins) for a particular page isn't too difficult, although it is not documented as far as I could tell. You do need to hook onto an action or filter before the "wp_head" runs. A good place is with the "wp_title" filter:
function my_title($title,$separator,$position) {
if (condition) {
remove_action('wp_head','jetpack_og_tags'); // JetPack
if (defined('WPSEO_VERSION')) { // Yoast SEO
global $wpseo_front;
remove_action('wp_head',array($wpseo_front,'head'),1);
}
if (defined('AIOSEOP_VERSION')) { // All-In-One SEO
global $aiosp;
remove_action('wp_head',array($aiosp,'wp_head'));
}
remove_action('wp_head','rel_canonical');
remove_action('wp_head','index_rel_link');
remove_action('wp_head','start_post_rel_link');
remove_action('wp_head','adjacent_posts_rel_link_wp_head');
add_action('wp_head','my_head',0);
// do whatever you need to $title here
}
return $title;
}
add_filter('wp_title','my_title',20,3);
Replace "condition" with whatever condition you use to check if this is the page to disable the SEO plugins on. In addition to disabling the Yoast SEO plugin and All-In-One SEO plugin, you see that we also disabled the JetPack open-graph tags, as well as the standard WordPress rel_canonical and rel_link actions. If you want to keep any of those actions, you should remove the appropriate lines.
Finally, the function adds your own 'wp_head' function - 'my_head' where you inject the meta tags, meta descriptions, open-graph tags, as you see fit.
3 comments
Comments are closed.