调用未定义的函数 convert_to_screen() [英] Call to undefined function convert_to_screen()

查看:19
本文介绍了调用未定义的函数 convert_to_screen()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个插件,我确实必须扩展类 WP_List_Table.我已经在我的插件文件中扩展了这个类(我不知道这是否是正确的方法?)并像这样包含 WP_List_Table:

I am developing a plugin where i did have to extend the class WP_List_Table. I have extended the class within my plugin-file (I don't know if this is the right way to do this?) and included WP_List_Table like this:

if(!class_exists('WP_List_Table')){
   require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}

然后是扩展类的代码,然后我像这样创建了我的表类的实例:

Then comes the code for extending the class and then I do create an instance of my table class like this:

<?php

 if ( ! class_exists( 'WP_List_Table' ) ) {
                require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}


 Class Wp_Ban_User extends WP_List_Table
 {

    public function __construct()
    {
             add_action('admin_menu',array($this,'WBU_adminMenu'));
             parent::__construct( array(
                  'singular'=> 'wp_list_text_link', //Singular label
                  'plural' => 'wp_list_test_links', //plural label, also this well be one of the table css class
                  'ajax'   => false //We won't support Ajax for this table
                  ) );      
            $this->prepare_items();
            $this->display();           

    }
     function get_columns() {
        $columns = array(
            'id'    => 'ID',
            'user_login'     => 'User Name',
            'user_email'   => 'User Email'            
        );
        return $columns;
    }


    function column_default( $item, $column_name ) {
        switch( $column_name ) {
            case 'id':
            case 'user_login':
            case 'user_email':

                return $item[ $column_name ];
            default:
                return print_r( $item, true ) ;
        }
    }
    function prepare_items() {

        $example_data = array(
                array(
                        'id'        => 1,
                        'user_login'     => 'vasim',
                        'user_email'    => 'vasim@abc.com'                        
                ),
                array(
                        'id'        => 2,
                        'user_login'     => 'Asma',
                        'user_email'    => 'Asma@abc.com'                        
                ),
                array(
                        'id'        => 3,
                        'user_login'     => 'Nehal',
                        'user_email'    => 'nehal@abc.com'                        
                ),
            );

        $columns = $this->get_columns();
        $hidden = array();
        $sortable = $this->get_sortable_columns();
        $this->_column_headers = array($columns, $hidden, $sortable);
        $this->items = $example_data;
    }

    public function WBU_adminMenu()
    {
            add_menu_page( 'Currently Logged In User', 'Banned User', 'manage_options', 'ban_admin_init', array($this,'ban_admin_init'));
    }
function ban_admin_init(){
        global $wpdb;

        $sql="SELECT * from {$wpdb->prefix}users";
        $sql_result=$wpdb->get_results($sql,'ARRAY_A');
        print_r($sql_result);
        //$this->items=$sql_result;     
    }

}

 global $Obj_Wp_Ban_User;

 $Obj_Wp_Ban_User=new Wp_Ban_User();

但是当我这样做时,我确实收到此错误:

But when i do this, i do get this error:

致命错误:调用未定义的函数 convert_to_screen() inD:\xampp\htdocs\developplugin\wp-admin\includes\class-wp-list-table.php在线 143

Fatal error: Call to undefined function convert_to_screen() in D:\xampp\htdocs\developplugin\wp-admin\includes\class-wp-list-table.php on line 143

我做了一些研究,但不知道如何解决它.

I did some research but did not understand how to fix it.

有人知道如何解决这个问题吗?

Does anybody knows how to fix this?

感谢您的帮助!

最好的问候.

推荐答案

抱歉我的英语不好,我是法语.

Sorry for my bad english, im french.

我发现了问题.您的课程已更正(参见代码底部):

I have found the problem. Your class corrected (see at the bottom of code):

<?php
/*
Plugin Name: My List Table Example
*/
 if ( ! class_exists( 'WP_List_Table' ) ) {
    require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}


Class Wp_Ban_User extends WP_List_Table
{

    public function __construct()
    {
             parent::__construct( array(
                  'singular'=> 'wp_list_text_link', //Singular label
                  'plural' => 'wp_list_test_links', //plural label, also this well be one of the table css class
                  'ajax'   => false //We won't support Ajax for this table
                  ) );      
            $this->prepare_items();
            $this->display();           

    }

    function get_columns() {
        $columns = array(
            'id'    => 'ID',
            'user_login'     => 'User Name',
            'user_email'   => 'User Email'            
        );
        return $columns;
    }

    function column_default( $item, $column_name ) {
        switch( $column_name ) {
            case 'id':
            case 'user_login':
            case 'user_email':

                return $item[ $column_name ];
            default:
                return print_r( $item, true ) ;
        }
    }

    function prepare_items() {

        $example_data = array(
                array(
                        'id'        => 1,
                        'user_login'     => 'vasim',
                        'user_email'    => 'vasim@abc.com'                        
                ),
                array(
                        'id'        => 2,
                        'user_login'     => 'Asma',
                        'user_email'    => 'Asma@abc.com'                        
                ),
                array(
                        'id'        => 3,
                        'user_login'     => 'Nehal',
                        'user_email'    => 'nehal@abc.com'                        
                ),
            );

        $columns = $this->get_columns();
        $hidden = array();
        $sortable = $this->get_sortable_columns();
        $this->_column_headers = array($columns, $hidden, $sortable);
        $this->items = $example_data;
    }

}


// Render your admin menu outside the class
function WBU_adminMenu()
{
    add_menu_page( 'Currently Logged In User', 'Banned User', 'manage_options', 'render_admin_page', 'render_admin_page');
}

// Create your menu outside the class
add_action('admin_menu','WBU_adminMenu');

// Render your page outside the class
function render_admin_page(){
    global $wpdb;

    $Obj_Wp_Ban_User=new Wp_Ban_User();
    $Obj_Wp_Ban_User->prepare_items();

    $sql="SELECT * from {$wpdb->prefix}users";
    $sql_result=$wpdb->get_results($sql,'ARRAY_A');
    print_r($sql_result);    
}

这个简单:为了解决错误Call to undefined function convert_to_screen(),你需要:

This simple : For resolve the error Call to undefined function convert_to_screen() you need to :

  • 在类外添加菜单
  • 在类外添加 admin_menu 操作
  • 在课堂外呈现您的管理页面

3 天后,它对我有用!

After 3 days, it's work for me !

这篇关于调用未定义的函数 convert_to_screen()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆