• Hi

    I am trying to create a custom admin page with WP_List_Table Class. However my checkboxs are not activated. Seems styles or java script issue. I am new to WordPress. Appreciate your help to fix this issue.

    This is my code to create a short code.

    if( ! class_exists( 'WP_List_Table' ) ) {
        require_once( ABSPATH . 'wp-admin/includes/screen.php' );
        require_once( ABSPATH . 'wp-admin/includes/class-wp-screen.php' );
        require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
        require_once( ABSPATH . 'wp-admin/includes/template.php' );
    }
    
    class User_List_Table extends WP_List_Table {
        // Contents of this function will execute when the blogger
        public $found_data = array();
           
        function __construct(){
            global $status, $page;
    
            parent::__construct( array(
                'singular'  => __( 'user', 'mylisttable' ),     //singular name of the listed records
                'plural'    => __( 'users', 'mylisttable' ),   //plural name of the listed records
                'ajax'      => false        //does this table support ajax?
            ) );
    
            add_action( 'admin_head', array( &$this, 'admin_header' ) );  
        }
      
        function get_data($args){
            //$args = array('role' => 'um_donee');
        $query = new WP_User_Query($args);
          $results = $query->get_results();
            $user_data = [];
            $i = 0;
            foreach ($results as $item) {
                $user_data[$i]["id"] = $item->ID;
                $user_data[$i]["user"] = '<a href="'. get_author_posts_url($item->ID) .'">'. get_avatar($item->user_email, 30) .'</a>';
                $user_data[$i]["display_name"] = $item->display_name;
                $user_data[$i]["user_email"] = $item->user_email;
                $user_data[$i]["description"] = get_user_meta($item->ID, 'description', true );
                $user_data[$i]["account_status"] = get_user_meta($item->ID, 'account_status', true );
                $i++;
            }
        return $user_data;
        }
        
        function admin_header() {
            $page = ( isset($_GET['page'] ) ) ? esc_attr( $_GET['page'] ) : false;
            if( 'my_list_test' != $page )
            return;
            echo '<style type="text/css">';
            echo '.wp-list-table .column-id { width: 5%; }';
            echo '.wp-list-table .column-user { width: 40%; }';
            echo '.wp-list-table .column-display_name { width: 35%; }';
            echo '.wp-list-table .column-email { width: 20%;}';
            echo '</style>';
        }
    
        function no_items() {
            _e( 'No users found !.' );
        }
      
        function column_default( $item, $column_name ) {
            switch( $column_name ) { 
                case 'user':
                case 'display_name':
                case 'user_email':
                case 'description':
                case 'account_status':
                    return $item[ $column_name ];
                default:
                    return print_r( $item, true ) ; //Show the whole array for troubleshooting purposes
            }
        }
    
        function get_sortable_columns() {
              $sortable_columns = array(
                'display_name'  => array('display_name',false),
                'user_email' => array('user_email',false),
                'account_status'   => array('account_status',false)
              );
              return $sortable_columns;
        }
    
        function get_columns(){
            $columns = array(
                'cb'        => '<input type="checkbox" />',
                'user' => __( 'User', 'mylisttable' ),
                'display_name'    => __( 'Display Name', 'mylisttable' ),
                'user_email'      => __( 'Email', 'mylisttable' ),
                'description'      => __( 'Summary', 'mylisttable' ),
                'account_status'      => __( 'Account Status', 'mylisttable' )
            );
             return $columns;
        }
        
        function usort_reorder( $a, $b ) {
          // If no sort, default to title
          $orderby = ( ! empty( $_GET['orderby'] ) ) ? $_GET['orderby'] : 'user';
          // If no order, default to asc
          $order = ( ! empty($_GET['order'] ) ) ? $_GET['order'] : 'asc';
          // Determine sort order
          $result = strcmp( $a[$orderby], $b[$orderby] );
          // Send final sort direction to usort
          return ( $order === 'asc' ) ? $result : -$result;
        }
       
        public function get_bulk_actions() {
          $actions = array(
            'delete'    => 'Delete',
            'edit' => 'Edit'
          );
          return $actions;
        }
      
        function column_cb($item) {
            return sprintf(
                '<input type="checkbox" name="users[]" value="%s"/>',
                $item['id']
            );    
        }
            
        function prepare_items() {
          $args = array( 'role' => 'um_donee' );
          $columns  = $this->get_columns();
          $hidden   = array();
          $sortable = $this->get_sortable_columns();
          $primary = 'cb';
          $user_data = $this->get_data($args);
          $this->_column_headers = array( $columns, $hidden, $sortable, $primary );
          usort( $user_data, array( &$this, 'usort_reorder' ) );
    
          $per_page = 5;
          $current_page = $this->get_pagenum();
          $total_items = count( $user_data );
    
          // only ncessary because we have sample data
          $this->found_data = array_slice( $user_data,( ( $current_page-1 )* $per_page ), $per_page );
    
          $this->set_pagination_args( array(
            'total_items' => $total_items,                  //WE have to calculate the total number of items
            'per_page'    => $per_page                     //WE have to determine how many items to show on a page
          ) );
          $this->items = $this->found_data;
        }
      
    }
    
    function list_donees($atts) {
      $donee_list = new User_List_Table();
      ob_start();
      echo '</pre><div class="wrap">';
      ?>
           <form method="post" action>
              <?php
               	$donee_list->prepare_items(); 
               	$donee_list->display(); 
       echo '</form></div>';
      return ob_get_clean();
    }
    
    add_shortcode('list_users', 'list_donees');
  • The topic ‘WP_List_Table Checkbox Column’ is closed to new replies.