[wp-hackers] Getting a call_user_func_array() error with add_(sub)?menu_page

Elizabeth Buckwalter elizabeth at slatetechpdx.com
Thu Feb 4 18:59:13 UTC 2010


I have a couple of plugins I'm working on.  I'm creating classes.  I copied
code from one plugin to the other.  One plugin runs fine, the other gives me
this error:

*Warning*: call_user_func_array() [function.call-user-func-array]: First
argument is expected to be a valid callback, 'MyPlugin::admin_loader' was
given in */var/www/vhosts/campdx.com/httpdocs/wp-includes/plugin.php* on
line *346*
*
*
*=============*
main plugin file:
---------------
one that works:
include(MWP_PLUGIN_DIR_PATH . 'my-working-plugin-class.php');
function fu_loaded() {
$fu = new MyWorkingPlugin;
add_action('admin_menu', array(&$fu, 'admin_page'));
add_action('admin_head', array(&$fu, 'admin_head'));

}
add_action('plugins_loaded', 'fu_loaded');

-------------------
the one that doesn't:
include(MYPLUGIN_DOCROOT . "classes/myplugin-class.php");
if (is_admin() ) {
include(MYPLUGIN_DOCROOT . "classes/myplugin-admin-class.php");
}
function myp_plugins_loaded () {
if ( is_admin() ) {
// To enable activation through the activate function
register_activation_hook(__FILE__,'dbem_install');
 // Execute the install script when the plugin is installed
add_action('activate_myplugin/myplugin.php','dbem_install');
 $mpadmin = new MyPlugin_Admin;
add_action('admin_menu',array(&$mpadmin, 'admin_page'));
 } else {
$dbem = new MyPlugins;
}
}
add_action('plugins_loaded', 'myp_plugins_loaded');

=======================
class definition:
------------------
one that works:

class MyWorkingPlugin {
function __construct() {
// some variables that needs to be instatiated
// __construct(blah = default, blah = default, blah = default
return $this;
 }
       // Thanks Session Manager for this code!
public function admin_page() {
$admin_page = 'admin/fu_admin.php';
$tab_title = __('My Working Plugin','fu');
$func = 'admin_loader';
$access_level = 'manage_options';
 $sub_pages = array(
__('Neat Stuff','fu')=>'fu_neat_stuff'
);
 add_menu_page($tab_title, $tab_title, $access_level, $admin_page,
array(&$this, $func));
 foreach ($sub_pages as $title=>$page) {
add_submenu_page($admin_page, $title, $title, $access_level, $page,
array(&$this, $func));
}
}
 public function admin_loader() {
$page = trim($this->get_request('page'));
 if ('admin/fu_admin.php' == $page) {
require_once(FU_ADMIN_DIR . 'fu_admin.php');
} else if (file_exists(FU_ADMIN_DIR . $page . '.php')) {
require_once(FU_ADMIN_DIR . $page . '.php');
}
}
}

----------------------------------
the one that doesn't:
// Renamed this plugin to the main plugin and changed appropriate code above
with the same results.
class MyPlugin_Admin extends MyPlugin {
 public function __construct() {
 return $this;
}

public function admin_page () {
$admin_page = 'admin/pages/main.php';
$tab_title = "My Plugin";
$func = 'admin_loader';
$access_level = 'manage_options';

add_menu_page($tab_title, $tab_title, $access_level, $admin_page,
array(&$this, $func));
 $sub_pages = array(
'Edit' => 'edit-form',
'Add New' => 'edit-form',
'Locations' => 'locations',
'People' => 'people',
'Categories'         => 'categories'
);
 foreach ($sub_pages as $title=>$page) {
add_submenu_page($admin_page, $title, $title, $access_level, $page,
array(&$this, $func));
}

}
 private function admin_loader() {
$page = trim($_GET['page']);
if ('admin/pages/main.php' == $page ) {
require_once(MYPLUGIN_DOCROOT . 'admin/pages/main.php');
} else {
require_once(MYPLUGIN_DOCROOT . 'admin/pages/' . $page . '.php');
}
}

}

I'm not seeing what the difference is.  I'm seeing differences of course,
but I just don't see how those could make a difference.  I've spent a couple
of hours, so far, toggling and tweaking and (un)commenting things to see
what would be causing this.

A Google search  comes up with nothing helpful.  I even looked through the
Drupal results that showed up, but didn't find an explanation. Google
Searches: 'call_user_func_array() add menu page wordpress',
'call_user_func_array() add menu page', 'call_user_func_array()
add_menu_page', 'call_user_func_array() object add_menu_page'

Oh yes.  I'm under NDA, so I've changed the names.  I checked the constant
names, but might have missed one.  Same for variables and Class names.

-- 
Thanks,
Elizabeth Buckwalter
Senior Developer
elizabeth at slatetechpdx.com
Slate Technologies LLC
http://slatetechpdx.com
6315 SE Holgate Blvd, Portland, OR
503.546.7698


More information about the wp-hackers mailing list