A Full WordPress Plugin Example — Part 4 Custom List Table Columns

In part 1, you have seen that WordPress creates a default table list for us after we add a custom post type. Apparently, we want to custom the table. In this part, we will add some custom columns “director” and “shortcode”.

The default movie table list:

GCMvoie list

Fortunately WordPress has provided a filter and an action to help us to do this. The theory is simple. The filter is used to add columns for a custom post type. And the action is used to get the output of a custom column for a custom post type.

Now let’s start the work.

Create admin Class

Create a file named class-gcmovie-admin.php and define a GCMovie_admin in it.

<?php
/*
 * GCMovie Admin
 */

if ( ! defined( 'WPINC' ) ) {
    die( 'No direct access.' );
}

class GCMovie_Admin {

}

Register filter and action

Define a constructor method and register our callbacks to manage_gcmovie_posts_columns filter and manage_gcmovie_posts_custom_column action in it.

    function __construct() {
        add_filter( 'manage_gcmovie_posts_columns', array( $this, 'custom_table_columns' ) );
        add_action( 'manage_gcmovie_posts_custom_column' , array( $this, 'custom_table_column' ), 10, 2 );  
    }

Implement filter callback

In our callback to manage_gcmovie_posts_columns filter, we add two more columns director and shortcode. The callback should return an array of all the columns will be displayed . For previous table has date column, so remember to add it and put it at the tail.

    function custom_table_columns( $columns ) {
        $date = $columns['date'];
        unset( $columns['date'] );

        $columns['director'] = __( 'Director', 'gcmoive' );
        $columns['shortcode'] = __( 'Shortcode', 'gcmoive' );
        $columns['date'] = $date;

        return $columns;
    }

Implement action callback

In our callback to manage_gcmovie_posts_custom_columns action, we define how to display output for each custom columns we’ve added.

    function custom_table_column( $column_name, $post_id ) {
        switch ( $column_name ) {
            case 'director' :
                echo esc_html( get_post_meta( $post_id, '_gcmovie_director', true ) );
                break;

            case 'shortcode' :
                echo "[gcmovie id=$post_id]";
                break;
        }
    }

Call init method

At the end of the file outside GCMovie_Admin class, remember to call its init().

class GCMovie_Admin {
    //...
}

GCMovie_Shortcode::init();

Include admin File in plugin main File

Add below code to the end of gcmovie.php to make the code take effect.

require_once GCMOVIE_PLUGIN_DIR . 'inc/class-gcmovie-admin.php';

Now our movie list looks like:

GCMoive custom list

Resources

Deprecated functions:

**Further customization to WordPress list table **

  • Using WP_List_Table to create wordpress admin tables

    You can extend a class from the WP_List_Table to create your own list table. Compared code your own table from the start, you will save much effort and benefit a lot from using that of WordPress. Many list tables like user list table, comment list table, they are implemented by extending WP_List_Table.