duanke6249 2015-05-28 13:13
浏览 169
已采纳

基于WP Plugin Boilerplate返回错误的插件“警告:call_user_func_array()期望参数1成为有效的回调”

I am building a Wordpress plugin based on the Wordpress Plugin Boilerplate (Object-Orientated Programming) and I am facing a difficulty when calling the function displaying my plugin admin page in my custom admin dashboard menu.

In my class I call for my custom menu method, and it works as the menu shows up great.

private function define_admin_hooks() {

    $plugin_admin = new myPlugin_Admin( $this->get_plugin_name(), $this->get_version() );

    $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
    $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );

    // Load CPTs
    $this->loader->add_action( 'admin_init', $plugin_admin, 'create_cpt_person' );
    $this->loader->add_action( 'admin_menu', $plugin_admin, 'setup_admin_menus' );
    $this->loader->add_action( 'parent_file', $plugin_admin, 'set_current_menu' );

}

And it then executes my setup_admin_menus method.

public static function setup_admin_menus() {

    $page_title = 'myPlugin';
    $menu_title = 'myPlugin';
    $capability = 'manage_options';
    $menu_slug  = 'myplugin_admin';
    $function   = 'display_admin_page'; // This is what triggers the showing of my admin page once I click on the parent menu (as if you were clicking on "Pages" in your admin dashboard)
    $icon_url   = 'dashicons-admin-page';
    $position   = 3;

    add_menu_page(
        $page_title,
        $menu_title,
        $capability,
        $menu_slug,
        $function,
        $icon_url,
        $position
        );

    $submenu_pages = array(
        // Post Type :: Person
        array(
            'parent_slug'   => 'myplugin_admin',
            'page_title'    => 'People',
            'menu_title'    => 'People',
            'capability'    => 'read',
            'menu_slug'     => 'edit.php?post_type=person',
            'function'      => null
            )
        );

    // Add each submenu item to custom admin menu.
    foreach($submenu_pages as $submenu){
        add_submenu_page(
            $submenu['parent_slug'],
            $submenu['page_title'],
            $submenu['menu_title'],
            $submenu['capability'],
            $submenu['menu_slug'],
            $submenu['function']
            );
    }
}

And this is the function called:

public static function display_admin_page() {

    echo '<div class="wrap">';
    echo '<h2>Welcome To WordPets</h2>';
    echo '<p>This is the custom admin page created from the custom admin menu.</p>';


    echo '</div><!-- end .wrap -->';
    echo '<div class="clear"></div>';
}

But whenever I go to my plugin's admin page, i get Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'display_admin_page' not found or invalid function name in /Users/Lazhar/dev/wp-includes/plugin.php on line 496 and it does not show anything but the error message.

Any help would be hugely appreciated :)

  • 写回答

2条回答 默认 最新

  • douhuang5331 2015-05-28 13:22
    关注

    Try to put your display_admin_page() method outside the class

    function display_admin_page() {
    
        echo '<div class="wrap">';
        echo '<h2>Welcome To WordPets</h2>';
        echo '<p>This is the custom admin page created from the custom admin menu.</p>';
    
    
        echo '</div><!-- end .wrap -->';
        echo '<div class="clear"></div>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题