Category: Uncategorized

Create Native Admin Tables In WordPress The Right Way

Create Native Admin Tables In WordPress The Right Way

    //Our class extends the WP_List_Table class, so we need to make sure that it's there
            require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );

    class Links_List_Table extends WP_List_Table {

             * Constructor, we override the parent to pass our own arguments
             * We usually focus on three parameters: singular and plural labels, as well as whether the class supports AJAX.
             function __construct() {
                     parent::__construct( array(
                    'singular'=> 'wp_list_text_link', //Singular label
                    'plural' => 'wp_list_test_links', //plural label, also this well be one of the table css class
                    'ajax'  => false //We won't support Ajax for this table
                    ) );

             * Add extra markup in the toolbars before or after the list
             * @param string $which, helps you decide if you add the markup after (bottom) or before (top) the list
            function extra_tablenav( $which ) {
                    if ( $which == "top" ){
                            //The code that goes before the table is here
                            echo"Hello, I'm before the table";
                    if ( $which == "bottom" ){
                            //The code that goes after the table is there
                            echo"Hi, I'm after the table";

             * Define the columns that are going to be used in the table
             * @return array $columns, the array of columns to use with the table
            function get_columns() {
                    return $columns= array(

             * Decide which columns to activate the sorting functionality on
             * @return array $sortable, the array of columns that can be sorted by the user
            public function get_sortable_columns() {
                    return $sortable = array(

             * Prepare the table with different parameters, pagination, columns and table elements
            function prepare_items() {
                    global $wpdb, $_wp_column_headers;
                    $screen = get_current_screen();

                    /* -- Preparing your query -- */
                $query = "SELECT * FROM $wpdb->links";

                    /* -- Ordering parameters -- */
                //Parameters that are going to be used to order the result
                $orderby = !empty($_GET["orderby"]) ? mysql_real_escape_string($_GET["orderby"]) : 'ASC';
                $order = !empty($_GET["order"]) ? mysql_real_escape_string($_GET["order"]) : '';
                if(!empty($orderby) & !empty($order)){ $query.=' ORDER BY '.$orderby.' '.$order; }

                    /* -- Pagination parameters -- */
            //Number of elements in your table?
            $totalitems = $wpdb->query($query); //return the total number of affected rows
            //How many to display per page?
            $perpage = 5;
            //Which page is this?
            $paged = !empty($_GET["paged"]) ? mysql_real_escape_string($_GET["paged"]) : '';
            //Page Number
            if(empty($paged) || !is_numeric($paged) || $paged<=0 ){ $paged=1; }
            //How many pages do we have in total?
            $totalpages = ceil($totalitems/$perpage);
            //adjust the query to take pagination into account
                    if(!empty($paged) && !empty($perpage)){
                    $query.=' LIMIT '.(int)$offset.','.(int)$perpage;

                    /* -- Register the pagination -- */
                    $this->set_pagination_args( array(
                            "total_items" => $totalitems,
                            "total_pages" => $totalpages,
                            "per_page" => $perpage,
                    ) );
                    //The pagination links are automatically built according to those parameters

                    /* — Register the Columns — */
                    $columns = $this->get_columns();
                    $hidden = array();
                    $sortable = $this->get_sortable_columns();
                    $this->_column_headers = array($columns, $hidden, $sortable);

                    /* -- Fetch the items -- */
                    $this->items = $wpdb->get_results($query);

             * Display the rows of records in the table
             * @return string, echo the markup of the rows
            function display_rows() {
                    //Get the records registered in the prepare_items method
                    $records = $this->items;

                    //Get the columns registered in the get_columns and get_sortable_columns methods
                    list( $columns, $hidden ) = $this->get_column_info();

                    //Loop for each record
                    if(!empty($records)){foreach($records as $rec){

                            //Open the line
                    echo '<tr id="record_'.$rec->link_id.'">';
                            foreach ( $columns as $column_name => $column_display_name ) {

                                    //Style attributes for each col
                                    $class = "class='$column_name column-$column_name'";
                                    $style = "";
                                    if ( in_array( $column_name, $hidden ) ) $style = ' style="display:none;"';
                                    $attributes = $class . $style;

                                    //edit link
                                    $editlink  = '/wp-admin/link.php?action=edit&link_id='.(int)$rec->link_id;

                                    //Display the cell
                                    switch ( $column_name ) {
                                            case "col_link_id":     echo '<td '.$attributes.'>'.stripslashes($rec->link_id).'</td>';        break;
                                            case "col_link_name": echo '<td '.$attributes.'><strong><a href="'.$editlink.'" title="Edit">'.stripslashes($rec->link_name).'</a></strong></td>'; break;
                                            case "col_link_url": echo '<td '.$attributes.'>'.stripslashes($rec->link_url).'</td>'; break;
                                            case "col_link_description": echo '<td '.$attributes.'>'.$rec->link_description.'</td>'; break;
                                            case "col_link_visible": echo '<td '.$attributes.'>'.$rec->link_visible.'</td>'; break;

                            //Close the line

    //Prepare Table of elements
    $wp_list_table = new Links_List_Table();

    //Table of elements


How to Replace WordPress Cron With A Real Cron Job

We are all too familiar with the “Missed Schedule” status, aren’t we? I am sure it is very frustrating to see that all your scheduled posts went MIA.

WordPress comes with its own cron job that allows you to schedule your posts and events. However, in many situations, the WP-Cron is not working well and leads to posts missed their publication schedule and/or scheduled events not executed.

To understand why this happen, we need to know that the WP-Cron is not a real cron job. It is in fact a virtual cron that only works when a page is loaded. In short, when a page is requested on the frontend/backend, WordPress will first load WP-Cron, follow by the necessary page to display to your reader. The loaded WP-Cron will then check the database to see if there is any thing that needs to be done.

Reasons for WP-Cron to fail could be due to:

  •     DNS issue in the server.
  •     Plugins conflict
  •     Heavy load in the server which results in WP-Cron not executed fully
  •     WordPress bug
  •     Using of cache plugins that prevent the WP-Cron from loading
  •     And many other reasons

There are many ways to solve the WP-Cron issue, but the one that I am going to propose here is to disable the virtual WP-Cron and use a real cron job instead.

Why use a real cron job?

By using a real cron job, you can be sure that all your scheduled items are executed. For popular blogs with high traffic, using a real cron job can also reduce the server bandwidth and reduce the chances of your server crashing, especially when you are experiencing Digg/Slashdot effect.

Scheduling a real cron job

To configure a real cron job, you will need access to your cPanel or Admin panel (we will be using cPanel in this tutorial).

1. Log into your cPanel.

2. Scroll down the list of applications until you see the “cron jobs” link. Click on it.


3. Under the Add New Cron Job section, choose the interval that you want it to run the cron job. I have set it to run every 15minutes, but you can change it according to your liking.


4. In the Command field, enter the following:

wget -q -O - >/dev/null 2>&1

Replace with your website URL.

5. Click the “Add New Cron Job” button. You should now see a message like this:

6. Next, using a FTP program, connect to your server and download the wp-config.php file.

7. Open the wp-config.php file with a text editor and paste the following line:

define('DISABLE_WP_CRON', true);

8. Save and upload (and replace) this file back to the server. This will disable WordPress internal cron job.

That’s it.

To test it out, it is best to schedule a post 5 – 10 minutes after you have completed the above configuration. Wait and see if the WordPress internal cron job is fired. If yes, then congratulation, you have successfully scheduled a real cron job for WordPress. If no, go through the steps above and see if you have made any mistake. Alternatively, contact your web host and get them to configure for you.