Skip to content

Instantly share code, notes, and snippets.

@amboutwe
Last active May 25, 2021 22:39
Show Gist options
  • Save amboutwe/1c847f9c706ff6f8c9eca76abea23fb6 to your computer and use it in GitHub Desktop.
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 );
}
}
}
@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