WordPress问题----致命错误:未捕获错误:调用未定义函数convert_to_screen()/wp-admin/includes/class-wp-list-table.php:149

时间:2019-05-07 18:13:10

标签: php wordpress

当我包含class-wp-list-table.php时,我会看到一个错误。

我正在使用composer自动加载psr-4,并且还声明了一个名称和平

致命错误:未捕获错误:调用/var/www/html/wordpress/building/wp-admin/includes/class-wp-list-table.php:149

中未定义的函数convert_to_screen()
   <?php
  /**
   *
   */

  namespace ems\apps\admin;

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

  class EmsListTable extends \WP_List_Table{


    public function prepare_items(){

      $orderby = isset($_GET['orderby']) ? trim($_GET['orderby']):"" ;
      $order =  isset($_GET['order']) ? trim($_GET['order']):""  ;
      $search_data = isset($_POST['s']) ? $_POST['s']:"";

      $datas = $this->wp_list_table_data_sample($orderby,$order,$search_data);

      $perpage = 2 ;
      $curent_page = $this->get_pagenum();
      $totalitems = count($datas);
      $this->set_pagination_args(array(
        'total_items' => $totalitems,
        'per_page' => $perpage
      ));
      $this->items = array_slice($datas,(($curent_page - 1) * $perpage),$perpage); // $datas;

      $columns = $this->get_columns();
      $hidden = $this->get_hidden_columns();
      $shortable = $this->get_sortable_columns();
      $this->_column_headers =  array($columns,$hidden,$shortable);
    }
    /*
    * all data process
    */
    public function wp_list_table_data_sample($orderby='',$order='',$search_data=''){
      global $wpdb;
      if (!empty($search_data)) {
        $allpost = $wpdb->get_results(
            "SELECT * FROM {$wpdb->posts}
            WHERE post_type='ems_events'
            AND post_status='publish'
            AND ( post_title LIKE '%$search_data%' OR post_content LIKE '%$search_data%' )"
        );
      }else{
        $allpost = get_posts(
          array(
            'post_type'   => 'ems_events',
            'post_status' => 'publish',
            'orderby'     => $orderby,
            'order'       => $order,
          )
        );
      }

      $post_array = array();

      if (count($allpost) > 0) {
        foreach ($allpost as $post) {
          // echo "<br>";
          // print_r($post);
          $post_array[] = array(
            "id"=>$post->ID,
            "title"=>$post->post_title,
            "content"=>$post->post_content,
            "name"=>$post->post_name,
          );
        }
      }

      return $post_array ;
    }
    /*hide some columns*/
    public function get_hidden_columns(){
      // return array('id');
    }

    public function get_sortable_columns(){
      return array(
        "title"=>array("title",false),
        "name"=>array("name",false)
      );
    }

    public function get_bulk_actions(){
      $actions = array(
        "delete"=> "Delete",
        "edit"=> "Edit"
      );
      return   $actions;
    }


    /*register columns*/
    public function get_columns(){
      $columns = array(
        "cb" => "<input type='checkbox' />",
        "id"=>"ID",
        "title"=>"Title",
        "content"=>"content",
        "name"=>"designation"
      );
      return $columns;
    }
    public function column_cb($id){
      return sprintf("<input type='checkbox' name='post[]' value='%s' />" , $id);
    }
    // show columns
    public function column_default($item,$column_name){
      switch ($column_name) {
        case 'id':
        case 'title':
        case 'content':
        case 'name':
          return $item[$column_name];
        default:
          return "No Value";
      }
    }

    public function column_title($item){
      $action = array(
        "edit" => sprintf('<a href="?page=%s&action=%s&post_id=%s">Edit</a>',$_GET['page'],'edit-koro',$item['id']),
        "delete" => sprintf('<a href="?page=%s&action=%s&post_id=%s">Delete</a>',$_GET['page'],'delete-holo',$item['id']),
      );
      return sprintf('%1$s %2$s',$item['title'],$this->row_actions($action));
    }

  }

1 个答案:

答案 0 :(得分:0)

我使用此代码解决了问题

    public function functuon_hooking()
{

    add_action( 'wp', [$this,'after_load_wordpress'] ); 
}

function after_load_wordpress(){
    new EventListTable(); 
}