-
-
Save wpchannel/7cdd6eed0927ea5732d7 to your computer and use it in GitHub Desktop.
<?php if (!defined('ABSPATH')) die('Restricted Area'); | |
/* | |
* Plugin Name: Disable Yoast SEO Notifications | |
* Description: Hide annoying notifications after each upgrade of Yoast SEO plugin and others admin notices. | |
* Version: 1.1 | |
* Author: Aurélien Denis | |
* Author URI: https://wpchannel.com/ | |
*/ | |
add_action('admin_init', 'wpc_disable_yoast_notifications'); | |
function wpc_disable_yoast_notifications() { | |
if (is_plugin_active('wordpress-seo/wp-seo.php')) { | |
remove_action('admin_notices', array(Yoast_Notification_Center::get(), 'display_notifications')); | |
remove_action('all_admin_notices', array(Yoast_Notification_Center::get(), 'display_notifications')); | |
} | |
} |
Thanks!
I'd suggest to don't get the Yoast_Notification_Center
instance twice and to check whether the class exists.
// Hide Yoast SEO alert from everywhere.
if ( is_plugin_active( 'wordpress-seo/wp-seo.php' ) ) {
// Remove HTML Comments.
add_action( 'get_header', function() {
ob_start( function( $o ) {
return preg_replace( '/\n?<.*?Yoast SEO plugin.*?>/mi', '', $o );
} );
} );
add_action( 'wp_head', function() { ob_end_flush(); }, 999 );
// Disable Yoast SEO Notifications.
function ntp_disable_yoast_notifications() {
if ( class_exists( 'Yoast_Notification_Center' ) ) {
$ync = Yoast_Notification_Center::get();
remove_action( 'admin_notices', array( $ync, 'display_notifications' ) ) ;
remove_action( 'all_admin_notices', array( $ync, 'display_notifications' ) );
}
}
add_action( 'admin_init', 'ntp_disable_yoast_notifications' );
// Yoast SEO Low Priority.
function ntp_yoast_bottom() {
return 'low';
}
add_filter('wpseo_metabox_prio', 'ntp_yoast_bottom');
// Disable screen after update.
function ntp_filter_yst_wpseo_option( $option ) {
if ( is_array( $option ) ) {
$option['seen_about'] = true;
}
return $option;
}
add_filter( 'option_wpseo', 'ntp_filter_yst_wpseo_option' );
// Remove Node in Toolbar.
function ntp_remove_yoast_bar( $wp_admin_bar ) {
$wp_admin_bar->remove_node( 'wpseo-menu' );
}
add_action( 'admin_bar_menu', 'ntp_remove_yoast_bar', 99 );
}
Since Yoast refuses to cut back on the the upsell spam, here's a snippet to remove all the "You trashed a thing! Buy our premium plugin!" notices that they clutter up the admin dashbaord with. Also recommend looking into the Hide SEO Bloat plugin which basically exists solely to remove all the crap that Yoast adds all over your site. Anyway, put this code in your functions.php (or use something like Code Snippets):
/**
* Unbind Yoast's awful constant upsell notifications whenever you trash/delete anything
*
* @ref: https://github.com/Yoast/wordpress-seo/blob/0742e9b6ba4c0d6ae9d65223267a106b92a6a4a1/admin/watchers/class-slug-change-watcher.php#L18
* @see: https://wordpress.stackexchange.com/a/352509
*/
function unbind_yoast_slug_change_watchers()
{
$priority = 10;
$actions_methods = [
'wp_trash_post' => 'detect_post_trash',
'before_delete_post' => 'detect_post_delete',
'delete_term_taxonomy' => 'detect_term_delete',
];
global $wp_filter;
foreach ($actions_methods as $action => $method)
{
if (isset($wp_filter[$action]->callbacks[$priority]) and ( ! empty($wp_filter[$action]->callbacks[$priority])))
{
$wp_filter[$action]->callbacks[$priority] = array_filter($wp_filter[$action]->callbacks[$priority], function($v, $k) use ($method) {
return (stripos($k, $method) === false);
}, ARRAY_FILTER_USE_BOTH );
}
}
}
add_action('plugins_loaded', 'unbind_yoast_slug_change_watchers', 20);
Hopefully this makes things a little nicer for anyone using it.
@primathon : thanks for sharing! For me the best solution was to switch to SEOPress. :)
❤️
Can it be the notice is now shown 'through' JS ?
I just noticed a pesky popup showing again after deleting a term (with the function @primathon shared in my functions).
@Beee4life I don't know because I'm using SEOPress PRO for the vast majority of my websites now. No ads.
@wpchannel then why reply if you don't know or even use the plugin (anymore)
Because they're the author of this gist, they get notifications for comments here, and they take responsibility for their work and want to help people that land here? Strange question.
If you mean to say "why reply if you can't answer my question" perhaps consider they did provide a solution, which is to use a different plugin.
We clearly have different opinions on what helpful replies are... because the question at hand is about a notice shown through js. Not whether I should use a different plugin or not... anyway, anyone who reads this, enjoy your day...
@Beee4life I'm the author of this gist, and I'm always trying to answer to comments even after years like I do on hundreds of tutorials published on my blog. The original problem to solve was to hide annoying notifications from Yoast plugin: this gist did the job for years. But now, the answer that I give to people - not only you in my previous answer, is simple: you don't like ads, change your SEO plugin to another. I'm tired of updating my mu-plugins every 6 months just to hide an advert. That's my solution, maybe you prefer another one and if you take time to find the correct hook, I'll be happy to update my gist with your code. ;)
@wpchannel Thank you, for formatting didn't seem to come through.
For anyone who want's to use this, drop this code in your theme's
function.php
file