How to create my own custom column

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


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 your own custom column 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-PLUGIN_NAME.php : Main plugin file that registers the column
  • /classes : The folder containing the separate logic classes
  • /classes/Column/Free/COLUMN_NAME.php : Column class with all column logic for the free version
  • /classes/Column/Pro/COLUMN_NAME.php : Column class with all column logic for the pro version
  • /classes/Editing/COLUMN_NAME.php : Editing Model with all editing-related logic
  • /classes/Export/COLUMN_NAME.php : Simple Export Model loaded through the Pro column
  • /classes/Filtering/COLUMN_NAME.php : Example Filtering Model loaded through Pro column
  • /classes/SmartFiltering/COLUMN_NAME.php : Example Smart Filtering Comparison (Model) loaded through Pro column
  • /classes/Sorting/COLUMN_NAME.php : Simple Sorting Model loaded through Pro column
  • /readme.txt : WordPress readme file to be used by the WordPress repository

Setup

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

  • PLUGIN_NAME : Single-word, no spaced. Underscores allowed. Used for the text-domain and plugin description.
  • CUSTOM_NAMESPACE : Single-word, no spaced. Underscores allowed. Used for the namespace declarations in the different class files.
  • 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 separated 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

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 AC/Column column, it’s time to customize the functionality.

The classes/Column/Free/COLUMN_NAME.php file has a couple of functions. Each of them is documented with internal comments, so please read them from the file.

Here is a quick overview of the functions:

  • __construct : Set the properties of the column, such as type, label or group
  • get_value : Returns the display value for the column.
  • get_raw_value : Returns the raw value for the column, used for sorting and editing.
  • is_valid : (optional) This determines whether the column should be available for a specific type, like a post or a page.
  • register_settings : (optional) Create extra settings for your column. These are visible when editing a column.
  • scripts : (optional) Enqueue CSS + JavaScript on the admin listings screen.

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


Make your column Pro

Features like filtering, sorting and inline edit are only available for the pro version of Admin Columns Pro. The /classes/Column/Pro/COLUMN_NAME.php file contains an implementation on how to make the column sortable and editable. You’ll need to implement an Editing and Sorting interface in your column class and create the associated models. This will allow you to enable sorting or editing in your column settings.