admin管理员组

文章数量:1122826

I have created a plugin with a top level page. I created a sub menu underneath that. When I run the plugin on my site everything works. But when I test it on a separate site I get an error and I'm not sure why.

This is the the code that is in question and the error that is getting thrown. I can't for the life of me figure out what the error is from.

function custom_plugin_add_menu_page(){

     add_menu_page( 'My Custom Plugin', 
                         'My Custom Plugin', 
                         'manage_options', 
                         'top_level_parent_page', 
                         'top_level_parent_page',
                         plugins_url( '/my-custom-plugin/includes/images/menu-icon.png' )
                         );                           

     add_submenu_page( 
        'top_level_parent_page', 
        'Custom Submenu Page', 
        'Custom Submenu Page',
        'manage_options', 
        'custom_plugin_submenu_page', 
        'custom_plugin_submenu_page_callback' 
    );                  
}
add_action('admin_menu', 'custom_plugin_add_menu_page');

and the error:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'custom_plugin_submenu_page_callback' not found or invalid function name in D:\xampp\htdocs\localtestsite\wp-includes\plugin.php on line 429

I have created a plugin with a top level page. I created a sub menu underneath that. When I run the plugin on my site everything works. But when I test it on a separate site I get an error and I'm not sure why.

This is the the code that is in question and the error that is getting thrown. I can't for the life of me figure out what the error is from.

function custom_plugin_add_menu_page(){

     add_menu_page( 'My Custom Plugin', 
                         'My Custom Plugin', 
                         'manage_options', 
                         'top_level_parent_page', 
                         'top_level_parent_page',
                         plugins_url( '/my-custom-plugin/includes/images/menu-icon.png' )
                         );                           

     add_submenu_page( 
        'top_level_parent_page', 
        'Custom Submenu Page', 
        'Custom Submenu Page',
        'manage_options', 
        'custom_plugin_submenu_page', 
        'custom_plugin_submenu_page_callback' 
    );                  
}
add_action('admin_menu', 'custom_plugin_add_menu_page');

and the error:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'custom_plugin_submenu_page_callback' not found or invalid function name in D:\xampp\htdocs\localtestsite\wp-includes\plugin.php on line 429

Share Improve this question edited Sep 26, 2018 at 13:12 T.Todua 5,8509 gold badges51 silver badges79 bronze badges asked Jan 12, 2014 at 12:29 EHermanEHerman 9992 gold badges16 silver badges33 bronze badges 2
  • Where is custom_plugin_submenu_page_callback? – s_ha_dum Commented Jan 12, 2014 at 14:57
  • Directly below it the add_action call. I've seen this issue when the function no longer exists, but I can see that it exists. It seems to throw this error on localhost only. Not on a live site. It's strange – EHerman Commented Jan 12, 2014 at 19:05
Add a comment  | 

2 Answers 2

Reset to default 0

It seems if you use classes you have to use array($this, $function)); in the function element as shown below.

add_submenu_page( $mainmenu_slug, $submenu_title, $submenu_label, $capability, $submenu_slug , array($this, $submenu_function) );   

problem 1

You have to declare the function custom_plugin_submenu_page_callback you are passing there

function custom_plugin_submenu_page_callback()
{ ?>
   hellooooo
<?php
}

obviously you have it on one site (maybe in functions.php) and you dont' have it on another site (or just the error's are turned off there).

problem 2

You have incorrect argument in add_menu_page - the 6th one should be the function name, not the icon name. You dont' get error on another site, because ERRORS are turned off there probably.

本文标签: create submenu pageerror function not found or invalid function name