1

enter image description here

function add_plugin_page_options(){

        ?>

            <div align = "center">
                    <h2>Usability Reviewer</h2>
                    <P>Main Page</P>
            </div>    

       <?php
}

I want to show data on this page from a table called issue. Columns are 'id' and 'issue'

2

I think you have two options to achieve what you are trying here.

Option 1: Using custom post types

This is the easiest route. WordPress does most of the work for you, while you can define your custom admin columns, add your post meta etc. WordPress will take of pagination and you can choose to make your custom post type posts private or public.

Where can you learn about post types?

See the codex for registering post types here: https://codex.wordpress.org/Function_Reference/register_post_type

Option 2: Using your own SQL database table

If you still prefer to do it with your own database table and have full control. You need t learn/make use of the WP_List_Table class. You can extend that class to display your custom SQL results.

Here's an example on extending the WP_List_Table.

<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

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

class Custom_List_Table extends WP_List_Table {

    /**
     * Initialize the table list.
     */
    public function __construct() {
        parent::__construct( array(
            'singular' => __( 'issue', 'textdomain' ),
            'plural'   => __( 'issues', 'textdomain' ),
            'ajax'     => false
        ) );
    }

    /**
     * Get list columns.
     *
     * @return array
     */
    public function get_columns() {
        return array(
            'cb'            => '<input type="checkbox" />',
            'id'            => __( 'ID', 'textdomain' ),
            'issue'         => __( 'Issue', 'textdomain' ),
        );
    }

    /**
     * Column cb.
     */
    public function column_cb( $issue ) {
        return sprintf( '<input type="checkbox" name="issue[]" value="%1$s" />', $issue['id'] );
    }

    /**
     * Return ID column
     */
    public function column_id( $issue ) {
        return '';
    }

    /**
     * Return issue column
     */
    public function column_issue( $issue ) {
        return '';
    }

    /**
     * Get bulk actions.
     *
     * @return array
     */
    protected function get_bulk_actions() {
        return array(

        );
    }

    /**
     * Prepare table list items.
     */
    public function prepare_items() {
        global $wpdb;

        $per_page = 10;
        $columns  = $this->get_columns();
        $hidden   = array();
        $sortable = $this->get_sortable_columns();

        // Column headers
        $this->_column_headers = array( $columns, $hidden, $sortable );

        $current_page = $this->get_pagenum();
        if ( 1 < $current_page ) {
            $offset = $per_page * ( $current_page - 1 );
        } else {
            $offset = 0;
        }

        $search = '';

        if ( ! empty( $_REQUEST['s'] ) ) {
            $search = "AND description LIKE '%" . esc_sql( $wpdb->esc_like( $_REQUEST['s'] ) ) . "%' ";
        }

        $items = $wpdb->get_results(
            "SELECT id, issue FROM YOUR_TABLE WHERE 1 = 1 {$search}" .
            $wpdb->prepare( "ORDER BY id DESC LIMIT %d OFFSET %d;", $per_page, $offset ), ARRAY_A
        );

        $count = $wpdb->get_var( "SELECT COUNT(id) FROM YOUR_TABLE WHERE 1 = 1 {$search};" );

        $this->items = $items;

        // Set the pagination
        $this->set_pagination_args( array(
            'total_items' => $count,
            'per_page'    => $per_page,
            'total_pages' => ceil( $count / $per_page )
        ) );
    }
}

Now you need to call this table in the output of your admin page (e.g. from your add_menu() function.)

$_table_list = new Custom_List_Table();
$_table_list->prepare_items();
echo '<input type="hidden" name="page" value="" />';
echo '<input type="hidden" name="section" value="issues" />';

$_table_list->views();
$_table_list->search_box( __( 'Search Key', 'textdomain' ), 'key' );
$_table_list->display();
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.