Skip to content

Instantly share code, notes, and snippets.

@amboutwe
Last active May 25, 2021 22:39
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save amboutwe/1c847f9c706ff6f8c9eca76abea23fb6 to your computer and use it in GitHub Desktop.
Remove Yoast from front end for a single post or page
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/* Remove Yoast from front end for a single post or page
* Credit: Dodinas https://stackoverflow.com/questions/37845968/disable-wordpress-yoast-seo-on-single-page
* Last Tested: Nov 08 2019 using Yoast SEO 12.4 on WordPress 5.2.4
* Note: For version 14+, please use the code here: https://developer.yoast.com/customization/yoast-seo/disabling-yoast-seo
*********
* DIFFERENT POST TYPES
* Post: Change 123456 to the post ID
* Page: Change is_single to is_page and 123456 to the page ID
* Custom Post Type: Change is_single to is_singular and 123456 to the 'post_type_slug'
Example: is_singular( 'cpt_slug' )
*********
* MULTIPLE ITEMS
* Multiple of the same type can use an array.
Example: is_single( array( 123456, 234567, 345678 ) )
* Multiple of different types can repeat the if statement
*/
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 );
}
}
}
@fabianrod
Copy link

Is not working for me, maybe there is any update of this code?

@amboutwe
Copy link
Author

@fabianrod This is not the proper place to request support. Please check out our extensive help section or visit the free support forum. If you require further support, upgrading to our premium version provides you with access to our support team.

@APlusDesign
Copy link

This GIST solution will not work with Yoast version 14 and above.

You now need to use.

$front_end = YoastSEO()->classes->get( Yoast\WP\SEO\Integrations\Front_End_Integration::class ); remove_action( 'wpseo_head', [ $front_end, 'present_head' ], -9999 );

@hannahmumma-zz
Copy link

Thanks @APlusDesign. That worked perfectly

@hirasso
Copy link

hirasso commented Feb 1, 2021

If you only deactivate the Yoast's Front_End_Integration, it will still hide the title_tag and some other built-in wordpress filters (front-end-integration.php, line 207 and following:

/**
 * Registers the appropriate hooks to show the SEO metadata on the frontend.
 *
 * Removes some actions to remove metadata that WordPress shows on the frontend,
 * to avoid duplicate and/or mismatched metadata.
 */
public function register_hooks() {
  
  \add_action( 'wp_head', [ $this, 'call_wpseo_head' ], 1 );
  // Filter the title for compatibility with other plugins and themes.
  \add_filter( 'wp_title', [ $this, 'filter_title' ], 15 );

  \add_action( 'wpseo_head', [ $this, 'present_head' ], -9999 );

  \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' );
  \remove_action( 'wp_head', 'noindex', 1 );
  \remove_action( 'wp_head', '_wp_render_title_tag', 1 );
  \remove_action( 'wp_head', 'gutenberg_render_title_tag', 1 );
}

Use this snippet to completely disable YOAST in the frontend:

add_action( 'plugins_loaded', 'disable_yoast_seo_frontend' );
function disable_yoast_seo_frontend() {
  if( is_admin() || !defined('WPSEO_VERSION') ) return;
  $loader = \YoastSEO()->classes->get( \Yoast\WP\SEO\Loader::class );
  \remove_action( 'init', [ $loader, 'load_integrations' ] );
  \remove_action( 'rest_api_init', [ $loader, 'load_routes' ] );
}

Tested with Yoast V. 15.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment