HOW to remove unwanted admin notices from WordPress admin screens?

As a plugin developer you feel frustrated when some other plugin will hijack your plugin screens with admin notices.

If you are one who is feeling the same, this code snippet will help you to remove all unwanted admin notices from your plugin admin screen

First you need to target admin_print_scripts WordPress action

add_action( 'admin_print_scripts', 'remove_admin_notices' );

function remove_other_admin_notices() {
    global $wp_filter;

    // Check whether is your plugin screen?
    if ( ! is_my_plugin_admin_screen() ) {
        return;
    }

    // Allow admin notices on your plugin admin screen 
    $es_display_notices = array(
        'show_review_notice',
    );

    // User admin notices
    if ( ! empty( $wp_filter['user_admin_notices']->callbacks ) && is_array( $wp_filter['user_admin_notices']->callbacks ) ) {
        foreach ( $wp_filter['user_admin_notices']->callbacks as $priority => $callbacks ) {
            foreach ( $callbacks as $name => $details ) {

                if ( is_object( $details['function'] ) && $details['function'] instanceof \Closure ) {
                    unset( $wp_filter['user_admin_notices']->callbacks[ $priority ][ $name ] );
                    continue;
                }

                if ( ! empty( $details['function'][0] ) && is_object( $details['function'][0] ) && count( $details['function'] ) == 2 ) {
                    $notice_callback_name = $details['function'][1];
                    if ( ! in_array( $notice_callback_name, $es_display_notices ) ) {
                        unset( $wp_filter['user_admin_notices']->callbacks[ $priority ][ $name ] );
                    }
                }
            }
        }
    }

    // Admin notices
    if ( ! empty( $wp_filter['admin_notices']->callbacks ) && is_array( $wp_filter['admin_notices']->callbacks ) ) {
        foreach ( $wp_filter['admin_notices']->callbacks as $priority => $callbacks ) {
            foreach ( $callbacks as $name => $details ) {

                if ( is_object( $details['function'] ) && $details['function'] instanceof \Closure ) {
                    unset( $wp_filter['admin_notices']->callbacks[ $priority ][ $name ] );
                    continue;
                }

                if ( ! empty( $details['function'][0] ) && is_object( $details['function'][0] ) && count( $details['function'] ) == 2 ) {
                    $notice_callback_name = $details['function'][1];
                    if ( ! in_array( $notice_callback_name, $es_display_notices ) ) {
                        unset( $wp_filter['admin_notices']->callbacks[ $priority ][ $name ] );
                    }
                }
            }
        }
    }

    // All admin notices
    if ( ! empty( $wp_filter['all_admin_notices']->callbacks ) && is_array( $wp_filter['all_admin_notices']->callbacks ) ) {
        foreach ( $wp_filter['all_admin_notices']->callbacks as $priority => $callbacks ) {
            foreach ( $callbacks as $name => $details ) {

                if ( is_object( $details['function'] ) && $details['function'] instanceof \Closure ) {
                    unset( $wp_filter['all_admin_notices']->callbacks[ $priority ][ $name ] );
                    continue;
                }

                if ( ! empty( $details['function'][0] ) && is_object( $details['function'][0] ) && count( $details['function'] ) == 2 ) {
                    $notice_callback_name = $details['function'][1];
                    if ( ! in_array( $notice_callback_name, $es_display_notices ) ) {
                        unset( $wp_filter['all_admin_notices']->callbacks[ $priority ][ $name ] );
                    }
                }
            }
        }
    }

    // Want to remove all notices from specific page?
    $get_page = ig_es_get_request_data( 'page' );
    if ( ! empty( $get_page ) && 'es_dashboard' == $get_page ) {
        remove_all_actions( 'admin_notices' );
    }
}

This will remove all admin notices.

Scroll to Top