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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配