Вопрос

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?

Это было полезно?

Решение

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.

Другие советы

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 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>

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с wordpress.stackexchange
scroll top