I’m using following code to display a notification in the admin dashboard. I want to display only on the main dashboard page (index.php), however the following code would display on all pages in the dashboard. Is there anyway to restrict it only to the dashboard main page? Thanks.

function my_admin_notice() { ?>
    <div class="updated">
        <p><?php _e( 'some message', 'my-text-domain' ); ?></p>
    </div>
    <?php
}
add_action( 'admin_notices', 'my_admin_notice' );

1 Answer
1

You can try the following:

function my_admin_notice() { ?>
    <div class="updated">
        <p><?php _e( 'some message', 'my-text-domain' ); ?></p>
    </div>
    <?php
}

add_action( 'load-index.php', 
    function(){
        add_action( 'admin_notices', 'my_admin_notice' );
    }
);

where we use the load-index.php hook to target the dashboard page.

Leave a Reply

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