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-column-template.php   : Main plugin file that registers the column
  • /classes   : The folder containing the separate logic classes
  • /classes/Column/Column.php   : Column class with all column logic
  • /classes/Column/Editing.php   : Editing Model with all editing-related logic
  • /classes/Colum/Export.php   : Export Model with all export-related logic
  • /classes/Column/Search.php   : Smart Filtering logic with all filtering-related logic
  • /classes/Column/Sorting.php   :  Sorting Model with all sorting-related logic
  • /readme.txt   : WordPress readme file to be used by the WordPress repository

Setup

This starter-kit uses two placeholdersLDERS   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

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


Hook Example

The starter-kit template will register the column for you. The hook used to register your column to Admin Columns looks something like this:

add_filter('ac/column/types/pro', 'acp_my_custom_column', 10, 2);

    // Register Column Factory
    function acp_my_custom_column(array $factories, AC\TableScreen $table_screen)
    {
    if ($table_screen instanceof AC\PostType) {
        $factories[] = MyCustomColumn::class;
    }
    
    return $factories;
}

Column Factory class

class MyCustomColumn extends ACP\Column\AdvancedColumnFactory
{

    public function get_label(): string
    {
        return 'My Custom Column Label';
    }

    public function get_column_type(): string
    {
        return 'ac-my-custom-column-type';
    }

    protected function get_formatters(AC\Setting\Config $config): AC\Setting\FormatterCollection
    {
        $formatters[] = new MyCustomFormatter();

        return new AC\Setting\FormatterCollection($formatters);
    }

    protected function get_editing(Config $config): ?ACP\Editing\Service
    {
        // Editing model
    }

    protected function get_sorting(Config $config): ?ACP\Sorting\Model\QueryBindings
    {
        // Sorting model
    }

    protected function get_search(Config $config): ?ACP\Search\Comparison
    {
        // Smart Filtering model
    }

    protected function get_export(Config $config): ?AC\Setting\FormatterCollection
    {
        // Export model
    }
}

Value formatter class. Renders the column value on the list table.

class MyCustomFormatter implements AC\Setting\Formatter
{

    public function format(AC\Type\Value $value)
    {
        // get meta value
        $meta_value = get_post_meta($value->get_id(), 'my_custom_field_key', true);

        return $value->with_value($meta_value);
    }
}

Add advanced features

Features like filtering, sorting and inline edit are only available for the pro version of Admin Columns Pro. You’ll need to implement an Editing, Filtering, Export and Sorting interface in your column class and create the associated models.

Still need help? Contact Us Contact Us