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.