【问题标题】:Wordpress WP List Table ImplemantationWordpress WP 列表实现
【发布时间】:2014-04-17 18:54:59
【问题描述】:

我在实现 WP List Table 时收到以下错误消息

致命错误:调用未定义的函数 convert_to_screen() .../wp-admin/includes/class-wp-list-table.php 在第 88 行

我已经通过添加以下行解决了这个问题

require_once(ABSPATH . 'wp-admin/includes/template.php' );

在这个 WP 列表在我的插件中运行良好之后。但是我在激活我的插件时收到了通知。

注意:convert_to_screen()、add_meta_box() 调用不正确。 可能直接包含 wp-admin/includes/template.php 以便 使用 add_meta_box()。这是非常错误的。挂钩 add_meta_box() 调用 改为 add_meta_boxes 操作。请参阅调试 WordPress 了解更多信息。 (此消息是在版本中添加的 3.3.) 在 /var/www/wordpress_RND/wordpress3.8.1/wp-includes/functions.php 上 第 3049 行

有没有人知道这个通知。我正在使用 Wordpress 版本 3.8.1

以下是要求的最小代码。

define('FRM_PLUGIN_DIR_PATH_INC', trailingslashit( plugin_dir_path( __FILE__ ) ) .'../inc');
if( ! class_exists( 'WP_List_Table' ) ) {
    require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
// Commenting below line result in error.
require_once(ABSPATH . 'wp-admin/includes/template.php' );

class Any_Forms_Funtions extends WP_List_Table{


    public function listForm(){
        echo "<div class='wrap'><form>";
        $this->prepare_items();
        $this->display();
        echo "</form></div>";
    }

    function get_columns(){
        $columns = array(
            //'cb'        => '<input type="checkbox" />',
            'booktitle' => 'Title',
            'author'    => 'Author',
            'isbn'      => 'ISBN',
        );
        return $columns;
    }

    function column_default( $item, $column_name ) {
        switch( $column_name ) {
            case 'booktitle':
            case 'author':
            case 'isbn':
                return $item[ $column_name ];
            default:
                return print_r( $item, true ) ; //Show the whole array for troubleshooting purposes
        }
    }

    function prepare_items() {
        $example_data = array(
            array('ID' => 1,'booktitle' => 'Quarter Share', 'author' => 'Rakesh','isbn' => '978-0982514542')
            ,array('ID' => 2,'booktitle' => 'Quarter Share', 'author' => 'Rakesh','isbn' => '978-0982514542')
        );
        $columns = $this->get_columns();
        $hidden = array();
        $sortable = $this->get_sortable_columns();
        $this->_column_headers = array($columns, $hidden, $sortable);
        $this->items = $example_data;
    }
}

如果还有其他需要,请告诉我。

【问题讨论】:

标签: php wordpress


【解决方案1】:

我所做的唯一更改是删除listForm 方法并添加__construct。无需 PHP 通知且不包含 wp-admin/includes/template.php 即可工作。

add_action('admin_menu', function () 
{
    add_menu_page(
        'TE', 
        '<span style="color:#e57300;">Table Example</span>', 
        'edit_pages', 
        'table-example', 
            function() { 
                echo '<div class="wrap">';
                echo '<h2>Table Example</h2>';
                new Table_SO_22371861(); 
                echo '</div>';
            },
        'http://sstatic.net/stackexchange/img/favicon.ico',
        1  // create before Dashboard menu item
    );
});
if( !class_exists( 'WP_List_Table' ) )
    require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );

class Table_SO_22371861 extends WP_List_Table{
    public function __construct()
    {
        parent::__construct( array(
            'singular' => 'table example',
            'plural'   => 'table examples',
            'ajax'     => true
        ) );
        $this->prepare_items();
        $this->display();
    }

    function get_columns(){
        $columns = array(
            //'cb'        => '<input type="checkbox" />',
            'booktitle' => 'Title',
            'author'    => 'Author',
            'isbn'      => 'ISBN',
        );
        return $columns;
    }

    function column_default( $item, $column_name ) {
        switch( $column_name ) {
            case 'booktitle':
            case 'author':
            case 'isbn':
                return $item[ $column_name ];
            default:
                return print_r( $item, true ) ; //Show the whole array for troubleshooting purposes
        }
    }

    function prepare_items() {
        $example_data = array(
            array('ID' => 1,'booktitle' => 'Quarter Share', 'author' => 'Rakesh','isbn' => '978-0982514542')
            ,array('ID' => 2,'booktitle' => 'Quarter Share', 'author' => 'Rakesh','isbn' => '978-0982514542')
        );
        $columns = $this->get_columns();
        $hidden = array();
        $sortable = $this->get_sortable_columns();
        $this->_column_headers = array($columns, $hidden, $sortable);
        $this->items = $example_data;
    }
}

查看以下扩展示例:I want a pagination to my options page of wordpress plugin?

【讨论】:

  • 我刚刚遇到了与错误通知相同的问题。我的问题是我在扩展 WP_List_Table 类后调用了我的新类,并在需要类时在我的代码中再次调用了我的新类。删除第一个实例化(紧跟在类扩展之后)修复了错误。
猜你喜欢
  • 1970-01-01
  • 2012-12-01
  • 2020-12-22
  • 1970-01-01
  • 2013-09-20
  • 1970-01-01
  • 2018-01-09
  • 1970-01-01
  • 2016-06-27
相关资源
最近更新 更多