4

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' );
4

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.

| improve this answer | |
  • Note: the function call back will only work in php 5.3 and above so use a function reference if you run into trouble declare a function and reference it as 'my_custom_notice_function' – hitautodestruct Mar 14 '16 at 12:59
  • thanks for that note and to those still running on an ancient PHP < 5.3, I would suggest upgrading asap ;-) @hitautodestruct – birgire Mar 14 '16 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.