0

I am adding an admin notice via the conventional admin_notice hook in my plugin:

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

if ( my_plugin_show_admin_notice() )
  add_action( 'admin_notices', 'my_admin_notice' );

How can I control where wordpress places the admin notice, in the html on the current screen, without using Javascript?

  • 2
    Can you elaborate where do you want to put the admin notice ? – Sumit Mar 14 '16 at 13:26
  • @sumit Sorry, I meant location as in the html on the page itself in a custom page. – hitautodestruct Mar 15 '16 at 4:51
  • You wrote this if ( /* some condition */ ) leads to confusions! – Sumit Mar 15 '16 at 5:05
  • @hitautodestruct i updated my answer to your new question. – iantsch Mar 15 '16 at 7:29
  • @Sumit I updated my answer. I just didn't want the focus to go away from the fact that I am asking about admin notices. – hitautodestruct Mar 15 '16 at 10:30
3

I found out by accident recently that all the notices will be moved to after the first <h2> tag on the page inside a <div class="wrap">. This gives you some slight control in that, for example, on a plugin settings page you can put an empty <h2></h2> at the very top if you want to use <h2>Plugin Title</h2> without it messing up the display formatting there.

  • This is the closest to controlling the placement. Wordpress supposedly does something internally to see where the <h*> tag is and places it there. – hitautodestruct Mar 31 '16 at 16:01
  • @majick nice catch +1 – iantsch Apr 1 '16 at 18:36
2

According to the Codex admin_notice does the following:

Notices displayed near the top of admin pages. The hook function should echo a message to be displayed.

That means you cannot place them at the bottom or middle of your screen, if you use the hook. Furthermore if you place a div with the class notice anywhere in your admin pages, WP admin JS scripts will put them just where all the other notices would be; and they will behave like notices too.

FYI: I tested this already with a fresh install. ;)

But

If you want to show a admin_notice at a particular page, you can add the action within a current_screen action:

function my_admin_notice() { ?>
    <div class="notice notice-info is-dismissible">
        <p><?php _e( 'some message', 'my_textdomain' ); ?></p>
    </div>
    <?php
}

function my_current_screen_example( $current_screen ) {
    // All your conditions go here, e.g. show notice @ post (not page) edit/new
    if ( 'post' == $current_screen->post_type && 'post' == $current_screen->base ) {
        add_action( 'admin_notices', 'my_admin_notice' );
    }
}

add_action( 'current_screen', 'my_current_screen_example' );

More Information:

  • I think you misunderstood the question ? :P – Sumit Mar 14 '16 at 13:27
  • @Sumit How come? His question after his code: How can I control where wordpress places the admin notice without using Javascript? – iantsch Mar 14 '16 at 13:30
  • Sorry my bad I did not see :) – Sumit Mar 14 '16 at 13:35
  • @iantsch Your answer is helpful but not what I intended. I want to control placement of the admin notice in the html. majick's answer helps me do that. – hitautodestruct Mar 31 '16 at 16:00
0

The action admin_notices is called or applied only once in the file, admin-header.php on line number 238, just at the beginning of the wpbody-content div, which makes up the body of a dashboard page. Hence, there is no way you can change the position of the admin notice, without using JavaScript. However, you can definitely make your own notice divs at the positions or locations you want, and manipulate their visibility.

Just create the mark up for the notice as:

<div class="notice notice-info is-dismissible">
    <p>This is a notice.</p>
</div>

You can now control it's visibility using PHP. Meaning when you redirect a user to this page, or submit a form on this page, just set the message in the div, and using a simple if else, echo the div.

0

I had the same problem and I resolved it by ensuring all the heading tags above where I want to place the notice there is no <h2>.

In this case, I wanted to display an error log notice below the input field as shown below : enter image description here

I solved it by introducing an empty <h2> tag just below the email input field and also change the tabs tag to <h3> instead of <h2>

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.