This guide will demonstrate how to create a new column type for the Admin Columns plugin.

Download

We have a free starter-kit available for creating a column for Admin Columns. This starter-kit contains all the necessary files to create a WP plugin and is heavily commented so you can build your column quickly and confidently.

Please download a copy of this starter-kit here: https://github.com/codepress/ac-column-template. (Click the Download Zip button )

After the starter-kit has downloaded, please extract the .zip file and move the folder called ac-column-template-master to your wp-content/plugins directory.

Structure

The starter-kit contains the following files and folders. Here is a brief explanation of each:

  • /css : folder for .css files
  • /js :folder for .js files
  • /languages : folder for .pot, .po and .mo files
  • ac-COLUMN_NAME.php : Main plugin file that registers the column
  • ac-column-COLUMN_NAME.php : Column class with all column logic
  • readme.txt : WordPress readme file to be used by the wordpress repository

Setup

This starter-kit usesPLACEHOLDERS such asCOLUMN_NAME throughout the file names and code. Use the following list of placeholders to do a ‘find and replace’:

  • COLUMN_NAME : Single word, no spaces. Underscores allowed. eg. my_column
  • COLUMN_LABEL : Multiple words, can include spaces, visible when selecting a column
  • PLUGIN_URL : Url to the github or WordPress repository
  • PLUGIN_TAGS : Comma seperated list of relevant tags
  • DESCRIPTION : Brief description of the field type, no longer than 2 lines
  • EXTENDED_DESCRIPTION : Extended description of the field type
  • AUTHOR_NAME : Name of field type author
  • AUTHOR_URL : URL to author’s website

Edit theac-COLUMN_NAME.php file (now renamed using your column name) and change the column type if necessary.

Edit theac-column-COLUMN_NAME.php file (now renamed using your column name) and include your custom code in the appropriate functions.

That’s it, your column type plugin is ready for testing! Please login to your WP website and activate the ac-COLUMN_NAME plugin. With the plugin activated, you will now see a new column type available when creating a new column in the Admin Columns plugin.

Customize

Once you created and activated your new column,  it’s time to customize the functionality.

The ac-column-COLUMN_NAME.php file has a couple of functions. Each of them is documented with internal comments, so please read them from the file ac-column-COLUMN_NAME.php.

Here is a quick overview of the functions:

  • init : Set the the properties of the column, such as type, label, support for sorting and inline-editing.
  • get_value : Returns the display value for the column.
  • get_raw_value : Returns the raw value for the column, used for sorting and editing.
  • apply_conditional : (Optional) This determines whether the column should be available for a specific type, like a post or a page.
  • display_settings : (Optional) Create extra settings for your column. These are visible when editing a column.
  • get_editable_settings : (Optional) Returns the settings used by inline-edit.
  • save : (Optional) Saves the value after using inline-edit.
  • scripts : (optional) Enqueue CSS + JavaScript on the admin listings screen.

Please read over the extended comments found above each function to learn more.

Enable sorting

To make your column support sorting make sure to set $this->properties['is_sortable'] = true; to true. The sorting value should be returned by the get_raw_value method. Make sure the returned value by this method is a string or integer, not an array. That’s it, Admin Columns will now use this value to sort.

Used Actions & Filters