• I have a custom table and I would like to display all the data from the table in admin area in a tabular format. I have searched everywhere , everyone shows the use of wp list class with a plugin. I am not building a plugin exactly.I have added menu pages from themes functions.php. So can anyone please help me on displaying the datas in admin area from my custom table?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Why don’t you want to use WP List Table? If you’re not comfortable coding it, then I’m afraid there’s no simple solution to just enabling an admin table for your custom table.

    And you are building a plugin. If you’re adding code to customise the WordPress backend or front end, then you’re building a plugin.

    Thread Starter debabratsharma

    (@debabratsharma)

    I tried using wp list table but datas are not getting displayed from my custom table.

    <?php

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

    /** Class constructor */
    public function __construct() {

    parent::__construct( [
    ‘singular’ => __( ‘Course’, ‘sp’ ), //singular name of the listed records
    ‘plural’ => __( ‘Courses’, ‘sp’ ), //plural name of the listed records
    ‘ajax’ => false //should this table support ajax?

    ] );

    }

    public static function get_customers( $per_page = 5, $page_number = 1 ) {

    global $wpdb;

    $sql = “SELECT * FROM wp_course”;

    if ( ! empty( $_REQUEST[‘orderby’] ) ) {
    $sql .= ‘ ORDER BY ‘ . esc_sql( $_REQUEST[‘orderby’] );
    $sql .= ! empty( $_REQUEST[‘order’] ) ? ‘ ‘ . esc_sql( $_REQUEST[‘order’] ) : ‘ ASC’;
    }

    $sql .= ” LIMIT $per_page”;

    $sql .= ‘ OFFSET ‘ . ( $page_number – 1 ) * $per_page;

    $result = $wpdb->get_results( $sql, ‘ARRAY_A’ );

    return $result;
    }

    public static function delete_customer( $id ) {
    global $wpdb;

    $wpdb->delete(
    “wp_course”,
    [ ‘ID’ => $id ],
    [ ‘%d’ ]
    );
    }
    public static function record_count() {
    global $wpdb;

    $sql = “SELECT COUNT(*) FROM wp_course”;

    return $wpdb->get_var( $sql );
    }

    public function no_items() {
    _e( ‘No courses avaliable.’, ‘sp’ );
    }

    function column_name( $item ) {

    // create a nonce
    $delete_nonce = wp_create_nonce( ‘sp_delete_customer’ );

    $title = ‘‘ . $item[‘name’] . ‘‘;

    $actions = [
    ‘delete’ => sprintf( ‘Delete‘, esc_attr( $_REQUEST[‘page’] ), ‘delete’, absint( $item[‘ID’] ), $delete_nonce )
    ];

    return $title . $this->row_actions( $actions );
    }

    public function column_default( $item, $column_name ) {
    switch ( $column_name ) {
    case ‘courseName’:
    case ‘fees’:
    return $item[ $column_name ];
    default:
    return print_r( $item, true ); //Show the whole array for troubleshooting purposes
    }
    }

    function column_cb( $item ) {
    return sprintf(
    ‘<input type=”checkbox” name=”bulk-delete[]” value=”%s” />’, $item[‘ID’]
    );
    }

    function get_columns() {
    $columns = [
    ‘cb’ => ‘<input type=”checkbox” />’,
    ‘courseName’ => __( ‘Course Name’, ‘sp’ ),
    ‘fees’ => __( ‘Fees’, ‘sp’ ),
    ‘duration’ => __( ‘Duration’, ‘sp’ )
    ];

    return $columns;
    }

    public function get_sortable_columns() {
    $sortable_columns = array(
    ‘courseName’ => array( ‘name’, true ),
    ‘fees’ => array( ‘city’, false )
    );

    return $sortable_columns;
    }

    public function get_bulk_actions() {
    $actions = [
    ‘bulk-delete’ => ‘Delete’
    ];

    return $actions;
    }

    public function prepare_items() {

    $this->_column_headers = $this->get_column_info();

    /** Process bulk action */
    $this->process_bulk_action();

    $per_page = $this->get_items_per_page( ‘customers_per_page’, 5 );
    $current_page = $this->get_pagenum();
    $total_items = self::record_count();

    $this->set_pagination_args( [
    ‘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 = self::get_customers( $per_page, $current_page );
    }

    public function process_bulk_action() {

    //Detect when a bulk action is being triggered…
    if ( ‘delete’ === $this->current_action() ) {

    // In our file that handles the request, verify the nonce.
    $nonce = esc_attr( $_REQUEST[‘_wpnonce’] );

    if ( ! wp_verify_nonce( $nonce, ‘sp_delete_customer’ ) ) {
    die( ‘Go get a life script kiddies’ );
    }
    else {
    self::delete_customer( absint( $_GET[‘customer’] ) );

    wp_redirect( esc_url( add_query_arg() ) );
    exit;
    }

    }

    // If the delete bulk action is triggered
    if ( ( isset( $_POST[‘action’] ) && $_POST[‘action’] == ‘bulk-delete’ )
    || ( isset( $_POST[‘action2’] ) && $_POST[‘action2’] == ‘bulk-delete’ )
    ) {

    $delete_ids = esc_sql( $_POST[‘bulk-delete’] );

    // loop over the array of record IDs and delete them
    foreach ( $delete_ids as $id ) {
    self::delete_customer( $id );

    }

    wp_redirect( esc_url( add_query_arg() ) );
    exit;
    }
    }

    }

    ?>

    This the class file that I created. After that on functions.php i used this–

    require_once ‘courses_display.php’;

    //Prepare Table of elements
    $wp_list_table = new Customers_List();
    $wp_list_table->prepare_items();
    $wp_list_table->display();

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Displaying data from custom table in wp admin area with edit & delete option’ is closed to new replies.