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

actions - admin_notices is not working inside function

programmeradmin3浏览0评论

I am creating a plugin . But when I trying to call the add_action('admin_notices','my_test_notice_1'); inside a function its not working.

I need to call the add_action('admin_notices','my_test_notice_1') inside my function . How can I do it. - Thanks advance for your answer.

<?php 
/*
* Plugin Name: Test Plugin
* Description: Testin the plugin description
* Version:     1.0.0
* Author:      Shafiq Suhag
* Author URI:  /
* License:     GPLv2 or later
* License URI: .0.html
* Text Domain: rsms
* Domain Path: /languages
*/

function test_plugin_admin_menu()
{        
    add_menu_page(__('Testing Page ', 'rses'), __('Testing Page', 'rses'), 'activate_plugins', 'testing_plugin', 'testing_plugin_cb_function','dashicons-businessman');
    function testing_plugin_cb_function(){

        // Admin Notice 2 : This is not working 
        add_action('admin_notices', 'admin_notice_2');
        function admin_notice_2(){
            echo '<h1> admin_notice_1  is printed on the page  </h1>';
        }
    }

    // Admin Notice 1 - This is working
    add_action('admin_notices', 'admin_notice_1');
    function admin_notice_1(){
        echo '<h1> admin_notice_1  is printed on the page  </h1>';
    }
}
add_action('admin_menu', 'test_plugin_admin_menu'); 

I am creating a plugin . But when I trying to call the add_action('admin_notices','my_test_notice_1'); inside a function its not working.

I need to call the add_action('admin_notices','my_test_notice_1') inside my function . How can I do it. - Thanks advance for your answer.

<?php 
/*
* Plugin Name: Test Plugin
* Description: Testin the plugin description
* Version:     1.0.0
* Author:      Shafiq Suhag
* Author URI:  https://roobnet/
* License:     GPLv2 or later
* License URI: https://www.gnu/licenses/gpl-2.0.html
* Text Domain: rsms
* Domain Path: /languages
*/

function test_plugin_admin_menu()
{        
    add_menu_page(__('Testing Page ', 'rses'), __('Testing Page', 'rses'), 'activate_plugins', 'testing_plugin', 'testing_plugin_cb_function','dashicons-businessman');
    function testing_plugin_cb_function(){

        // Admin Notice 2 : This is not working 
        add_action('admin_notices', 'admin_notice_2');
        function admin_notice_2(){
            echo '<h1> admin_notice_1  is printed on the page  </h1>';
        }
    }

    // Admin Notice 1 - This is working
    add_action('admin_notices', 'admin_notice_1');
    function admin_notice_1(){
        echo '<h1> admin_notice_1  is printed on the page  </h1>';
    }
}
add_action('admin_menu', 'test_plugin_admin_menu'); 
Share Improve this question edited Apr 28, 2020 at 5:20 Chetan Vaghela 2,4084 gold badges10 silver badges16 bronze badges asked Apr 27, 2020 at 21:54 Shafiqul IslamShafiqul Islam 32 bronze badges 1
  • You are hooking "admin_notice_2()" function inside callback of "add_menu_page()". That is not correct. – Nilambar Sharma Commented Apr 28, 2020 at 4:52
Add a comment  | 

1 Answer 1

Reset to default 0

Well, this is happening because of hook firing sequence. Hooks actually get loaded from an array(have a look) where admin_notices executes before add_menu_page gets called.

Now to show you message in your admin page you can check global $pagenow and the page GET variable by $_GET['page'] with an if condition and print your message. Like below-

add_action( 'admin_menu', 'test_plugin_admin_menu' ); 
function test_plugin_admin_menu () {
    add_menu_page(
        __('Testing Page ', 'rses'),
        __('Testing Page', 'rses'),
        'activate_plugins',
        'testing_plugin',
        'testing_plugin_cb_function',
        'dashicons-businessman'
    );
}

function testing_plugin_cb_function () {
    // Do your magic here
}

add_action( 'admin_notices', 'admin_notice_1' );
function admin_notice_1 () {
    global $pagenow;
    if ( 'admin.php' === $pagenow && 'testing_plugin' === $_GET['page'] ) {
        echo '<h1> admin_notice_1  is printed on the page  </h1>';
    }
}

By the way, nested functions are quite a bad thing, so I'm putting here a refactored version of your code. Please test it before putting it in production.

Hope that helps.

发布评论

评论列表(0)

  1. 暂无评论