I need to show notice on wordpress Dashboard only

I am trying to make a notice on wordpress dashboard only. but when i paste this code in function.php notice show on all the pages like, Post , Authors , Pages everywhere. I just want only show on main dashboard Page.

function sample_admin_notice__success() {
    ?>
    <div class="notice notice-success is-dismissible">
        <p><?php _e( 'Done!', 'sample-text-domain' ); ?></p>
    </div>
    <?php
}
add_action( 'admin_notices', 'sample_admin_notice__success' );

1 answer

  • answered 2021-10-13 13:54 Screenload

    You can check for the current screen

    function sample_admin_notice__success() {
        ?>
        <div class="notice notice-success is-dismissible">
            <p><?php _e( 'Done!', 'sample-text-domain' ); ?></p>
        </div>
        <?php
    }
    
    if (is_admin()) {
        $screen = get_current_screen();
        
        if ($screen -> id == "dashboard") {
            add_action( 'admin_notices', 'sample_admin_notice__success' );
        }
    }
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum