Dodinas
Dodinas

Reputation: 6805

Disable Wordpress Yoast SEO on Single Page

I'm attempting to disable the Wordpress Yoast SEO on a single page because it's conflicting with a different plugin.

I tried following this StackOverflow question, adding this code to functions.php:

add_action('template_redirect','remove_wpseo');

function remove_wpseo(){
    if ( is_page(944)) {
      global $wpseo_front;
      remove_action( 'wp_head', array($wpseo_front, 'head'), 2 ); // <-- check priority
    }
}

The above did not work, so I then ran across this post, and tried to change it to below, which of course resulted in a 500 error.

add_action('template_redirect','remove_wpseo');

function remove_wpseo(){
   if ( is_page(5526)) {
     global WPSEO_Frontend::get_instance()
     remove_action( 'wp_head', array(WPSEO_Frontend::get_instance(), 'head'), 2 ); // <-- check priority
   }
}

Any ideas on how I might go about disabling Yoast SEO on a single page? Should I do this from functions.php or somewhere else? I think I'm close, but not quite there.

Upvotes: 5

Views: 5988

Answers (2)

jhashane
jhashane

Reputation: 770

As of Yoast version 14.0, They have changed the way of disabling Yoast SEO output. This is the new method.

add_action( 'template_redirect', 'remove_wpseo' );

function remove_wpseo() {
    if ( is_page ( 5526 ) ) {

       $front_end = YoastSEO()->classes->get( Yoast\WP\SEO\Integrations\Front_End_Integration::class );

       remove_action( 'wpseo_head', [ $front_end, 'present_head' ], -9999 );
    }
}

Hope this helps!

Upvotes: 2

Dodinas
Dodinas

Reputation: 6805

Okay, I figured out what I was doing wrong. Here is the corrected code which is working:

add_action('template_redirect','remove_wpseo');

function remove_wpseo(){
    if (is_page(5526)) {
        global $wpseo_front;
            if(defined($wpseo_front)){
                remove_action('wp_head',array($wpseo_front,'head'),1);
            }
            else {
              $wp_thing = WPSEO_Frontend::get_instance();
              remove_action('wp_head',array($wp_thing,'head'),1);
            }
    }
}

Thanks!

Upvotes: 11

Related Questions