Aggressively block all notifications in WordPress Backend

Hi,

we wanted to reduce our plugin and instead of relying on one, we created the following snippet. It blocks every and all notices in the WP Backend (though that includes “Saved Post”, and other WP-own notifications). Add the following snippet to your functions.php, or via a Plugin.

function absolutely_disable_all_admin_notices() {
    global $wp_filter;
    
    // Remove all admin notices
    if (is_array($wp_filter['admin_notices']->callbacks)) {
        foreach ($wp_filter['admin_notices']->callbacks as $priority => $callbacks) {
            unset($wp_filter['admin_notices']->callbacks[$priority]);
        }
    }
    
    // Remove all network admin notices (for multisite)
    if (is_array($wp_filter['network_admin_notices']->callbacks)) {
        foreach ($wp_filter['network_admin_notices']->callbacks as $priority => $callbacks) {
            unset($wp_filter['network_admin_notices']->callbacks[$priority]);
        }
    }
    
    // Remove all user admin notices
    if (is_array($wp_filter['user_admin_notices']->callbacks)) {
        foreach ($wp_filter['user_admin_notices']->callbacks as $priority => $callbacks) {
            unset($wp_filter['user_admin_notices']->callbacks[$priority]);
        }
    }
    
    // Remove all custom admin notices added by plugins or themes
    if (is_array($wp_filter['all_admin_notices']->callbacks)) {
        foreach ($wp_filter['all_admin_notices']->callbacks as $priority => $callbacks) {
            unset($wp_filter['all_admin_notices']->callbacks[$priority]);
        }
    }
}

// Hook into WordPress as early as possible
add_action('admin_init', 'absolutely_disable_all_admin_notices', 1);

// Use output buffering to catch any notices that might be printed directly
function start_admin_notice_buffer() {
    ob_start();
}
add_action('admin_notices', 'start_admin_notice_buffer', -9999);

function end_admin_notice_buffer() {
    ob_end_clean();
}
add_action('admin_notices', 'end_admin_notice_buffer', 9999);

// Aggressively hide notices using CSS
function aggressively_hide_admin_notices_css() {
    echo '<style>
        .notice, 
        .updated, 
        .update-nag, 
        .error, 
        .is-dismissible, 
        .wdan-notice-wrapper,
        #message,
        .notice-warning,
        .notice-info,
        .notice-success,
        .notice-error,
        #setting-error-settings_updated,
        .update-message,
        .updated-message,
        .woocommerce-message,
        .yoast-alert,
        .yoast-container,
        .wp-core-ui .notice.is-dismissible,
        .notice-dismiss,
        div.error,
        div.updated { 
            display: none !important; 
            visibility: hidden !important;
            opacity: 0 !important;
            height: 0 !important;
            padding: 0 !important;
            margin: 0 !important;
        }
    </style>';
}
add_action('admin_head', 'aggressively_hide_admin_notices_css');
add_action('login_head', 'aggressively_hide_admin_notices_css');

// Disable admin notices for specific screens
function disable_admin_notices_for_specific_screens() {
    $screen = get_current_screen();
    if (isset($screen->id) && in_array($screen->id, array('dashboard', 'plugins', 'themes', 'options-general'))) {
        remove_all_actions('admin_notices');
        remove_all_actions('all_admin_notices');
    }
}
add_action('current_screen', 'disable_admin_notices_for_specific_screens');

// Prevent notice output in the footer
function remove_footer_admin() {
    echo '';
}
add_action('admin_footer_text', 'remove_footer_admin');

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *