How to disable Yoast SEO in specific page?

i'm trying to hide the title tag of the head of my template. I'm using this code:

add_action( 'template_redirect', 'remove_wpseo' );
function remove_wpseo() {
    if ( is_single ( 123456 ) ) {
        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 );
        }
    }
}

But it does not work.

Upvotes: 1

Views: 2066

Answers (3)

Jonas Lundman
Jonas Lundman

Reputation: 1490

As of Yoast SEO 14.0, disable all Yoast output. Warning, the tags are removed. Taken from Yoast docs:

function intervik_theme_wpseo_remove(){
    if(function_exists('YoastSEO')){
        $front_end = YoastSEO()->classes->get( Yoast\WP\SEO\Integrations\Front_End_Integration::class );
        if ( is_single ( 123456 ) ) remove_action('wpseo_head', [ $front_end, 'present_head' ], -9999 );
    }
}
add_action('template_redirect', 'intervik_theme_wpseo_remove');

However, you might take a look at the new API, removing Title only:

function intervik_wpseo_frontend_presenters($presenters){
    if ( !is_single ( 123456 ) ) return $presenters;
    /* REMOVE ONE exactly presenters */
    if(($key = array_search('Yoast\WP\SEO\Presenters\Twitter\Title_Presenter', $presenters)) !== false){
        unset($presenters[$key]);
    }
    return $presenters;
}
add_filter('wpseo_frontend_presenter_classes', 'intervik_wpseo_frontend_presenters', 10, 1);

Removing all default social_meta related tags:

function intervik_wpseo_frontend_presenters($presenters){

    /* return all WITHOUT Open_Graph and Twitter presenters */
    
    if($matches = preg_grep('/Open_Graph|Twitter/', $presenters)) return array_diff($presenters, $matches);
        else return $presenters;

}
add_filter('wpseo_frontend_presenter_classes', 'intervik_wpseo_frontend_presenters', 10, 1);

Where presenters are for example (dump):

array(27) {
  [0]=>
  string(39) "Yoast\WP\SEO\Presenters\Title_Presenter"
  [1]=>
  string(50) "Yoast\WP\SEO\Presenters\Meta_Description_Presenter"
  [2]=>
  string(40) "Yoast\WP\SEO\Presenters\Robots_Presenter"
  [3]=>
  string(43) "Yoast\WP\SEO\Presenters\Googlebot_Presenter"
  [4]=>
  string(41) "Yoast\WP\SEO\Presenters\Bingbot_Presenter"
  [5]=>
  string(43) "Yoast\WP\SEO\Presenters\Canonical_Presenter"
  [6]=>
  string(42) "Yoast\WP\SEO\Presenters\Rel_Prev_Presenter"
  [7]=>
  string(42) "Yoast\WP\SEO\Presenters\Rel_Next_Presenter"
  [8]=>
  string(51) "Yoast\WP\SEO\Presenters\Open_Graph\Locale_Presenter"
  [9]=>
  string(49) "Yoast\WP\SEO\Presenters\Open_Graph\Type_Presenter"
  [10]=>
  string(50) "Yoast\WP\SEO\Presenters\Open_Graph\Title_Presenter"
  [11]=>
  string(56) "Yoast\WP\SEO\Presenters\Open_Graph\Description_Presenter"
  [12]=>
  string(48) "Yoast\WP\SEO\Presenters\Open_Graph\Url_Presenter"
  [13]=>
  string(54) "Yoast\WP\SEO\Presenters\Open_Graph\Site_Name_Presenter"
  [14]=>
  string(62) "Yoast\WP\SEO\Presenters\Open_Graph\Article_Publisher_Presenter"
  [15]=>
  string(59) "Yoast\WP\SEO\Presenters\Open_Graph\Article_Author_Presenter"
  [16]=>
  string(67) "Yoast\WP\SEO\Presenters\Open_Graph\Article_Published_Time_Presenter"
  [17]=>
  string(66) "Yoast\WP\SEO\Presenters\Open_Graph\Article_Modified_Time_Presenter"
  [18]=>
  string(50) "Yoast\WP\SEO\Presenters\Open_Graph\Image_Presenter"
  [19]=>
  string(54) "Yoast\WP\SEO\Presenters\Open_Graph\FB_App_ID_Presenter"
  [20]=>
  string(46) "Yoast\WP\SEO\Presenters\Twitter\Card_Presenter"
  [21]=>
  string(47) "Yoast\WP\SEO\Presenters\Twitter\Title_Presenter"
  [22]=>
  string(53) "Yoast\WP\SEO\Presenters\Twitter\Description_Presenter"
  [23]=>
  string(47) "Yoast\WP\SEO\Presenters\Twitter\Image_Presenter"
  [24]=>
  string(49) "Yoast\WP\SEO\Presenters\Twitter\Creator_Presenter"
  [25]=>
  string(46) "Yoast\WP\SEO\Presenters\Twitter\Site_Presenter"
  [26]=>
  string(40) "Yoast\WP\SEO\Presenters\Schema_Presenter"
}

Upvotes: 1

Lawraoke
Lawraoke

Reputation: 556

You may try to visit the link here

Moreover, Try Yoast solution here

Hope this helps!

Upvotes: 0

Gulshan kumar
Gulshan kumar

Reputation: 407

It will work. please add the page id in the condition like below:-

Note:- This is for post detail page if ( is_single ( 123456 ) )

Please add below code in function.php and don't forget to change page id with specific page from which you want to remove.

if ( get_the_ID()==96 )

add_action( 'template_redirect', 'remove_wpseo' );
function remove_wpseo() {
    if ( get_the_ID()==96 ) {
        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 );
        }
    }
}

Upvotes: 1

Related Questions