Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress WP List Table Implemantation

Tags:

php

wordpress

I was getting following error message while implementing WP List Table

Fatal error: Call to undefined function convert_to_screen() in .../wp-admin/includes/class-wp-list-table.php on line 88

I have resolved this problem by including following line

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

After this WP list table is working good in my plug-in. But i am getting below notice while activating my plugin.

Notice: convert_to_screen(), add_meta_box() was called incorrectly. Likely direct inclusion of wp-admin/includes/template.php in order to use add_meta_box(). This is very wrong. Hook the add_meta_box() call into the add_meta_boxes action instead. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in /var/www/wordpress_RND/wordpress3.8.1/wp-includes/functions.php on line 3049

Does any one have any idea about this notice. I am using Wordpress Version 3.8.1

below is the minimal code as requested.

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;
    }
}

Please let me know if any this else needed.

like image 668
Trimantra Software Solution Avatar asked Jul 04 '26 15:07

Trimantra Software Solution


1 Answers

The only change I made was removing the listForm method and adding the __construct. Works without PHP notices and without including 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;
    }
}

Check the following for an extended example: I want a pagination to my options page of wordpress plugin?

like image 74
brasofilo Avatar answered Jul 07 '26 05:07

brasofilo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!