草庐IT

php - 调用未定义函数 convert_to_screen()

coder 2024-04-17 原文

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

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

然后是扩展类的代码,然后我创建了一个表类的实例,如下所示:

<?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();

但是当我这样做时,我确实得到了这个错误:

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

我做了一些研究,但不知道如何修复它。

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

感谢您的帮助!

最好的问候。

最佳答案

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

我发现了问题。您的类(class)已更正(请参阅代码底部):

<?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() 你需要:

  • 在类外添加菜单
  • 在类外添加admin_menu Action
  • 在类外呈现您的管理页面

3 天后,它对我有用!

关于php - 调用未定义函数 convert_to_screen(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40867316/

有关php - 调用未定义函数 convert_to_screen()的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  3. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  4. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  5. ruby-on-rails - rails : save file from URL and save it to Amazon S3 - 2

    从给定URL下载文件并立即将其上传到AmazonS3的更直接的方法是什么(+将有关文件的一些信息保存到数据库中,例如名称、大小等)?现在,我既不使用Paperclip,也不使用Carrierwave。谢谢 最佳答案 简单明了:require'open-uri'require's3'amazon=S3::Service.new(access_key_id:'KEY',secret_access_key:'KEY')bucket=amazon.buckets.find('image_storage')url='http://www.ex

  6. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  7. ruby-on-rails - rails : How to make a form post to another controller action - 2

    我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak

  8. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  9. ruby-on-rails - link_to 不显示任何 rails - 2

    我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article

  10. ruby - 无法覆盖 irb 中的 to_s - 2

    我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)

随机推荐