最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Troubleshooting Admin_Notice

programmeradmin0浏览0评论

I have this code at the top of a plugin:

 function my_mwe_admin_notice(){
    echo '<div class="notice notice-error">';
    echo '<h1>Notice this.</h1>';
    echo '</div>';
 }
add_action( 'admin_notices', 'my_mwe_admin_notice' );

Where and when is this notice supposed to appear?

I can't find it.

Have also tried adding global $pagenow and if ( 'plugins.php' == $pagenow ) { // also index.php, etc...

What am I missing?

I have this code at the top of a plugin:

 function my_mwe_admin_notice(){
    echo '<div class="notice notice-error">';
    echo '<h1>Notice this.</h1>';
    echo '</div>';
 }
add_action( 'admin_notices', 'my_mwe_admin_notice' );

Where and when is this notice supposed to appear?

I can't find it.

Have also tried adding global $pagenow and if ( 'plugins.php' == $pagenow ) { // also index.php, etc...

What am I missing?

Share Improve this question asked Jun 11, 2020 at 20:39 MikeiLLMikeiLL 5791 gold badge8 silver badges22 bronze badges 6
  • 3 It will appear only in the back end, in wp-admin at the top of any page, not on the front end. – shanebp Commented Jun 11, 2020 at 22:39
  • Ha ha. I know that. You mean in the admin console, right? The above code should display <h1>Notice this.</h1> on every page in the admin, shouldn't it? – MikeiLL Commented Jun 11, 2020 at 23:52
  • 2 Yes. btw - tested your code and it works properly. – shanebp Commented Jun 12, 2020 at 4:07
  • Any ideas for troubleshooting why it's not working on my dev env? – MikeiLL Commented Jun 12, 2020 at 20:10
  • No. Is the rest of the plugin working? – shanebp Commented Jun 12, 2020 at 21:10
 |  Show 1 more comment

1 Answer 1

Reset to default 0

Yup. I threw my test code in the top of the Hello Dolly plugin and there was the notice.

That's when I remembered that I was calling it within a namespace:

So needed to referenced that so the WP action could access it:

namespace My_Plugin;

function my_mwe_admin_notice(){
    echo '<div class="notice notice-error">';
    echo '<h1>Notice this.</h1>';
    echo '</div>';
 }
add_action( 'admin_notices', __NAMESPACE__ . '\\my_mwe_admin_notice' );
发布评论

评论列表(0)

  1. 暂无评论