Class: ColumnFactory

ColumnFactory  is the blueprint for a column type. Admin Columns calls it once for each column on a table to build a column object: it reads the type and label, collects the formatters that produce the displayed value, and (with Pro) attaches editing, sorting, filtering, export, conditional formatting, and metric behavior. You create your own column type by extending one of these classes and overriding the methods you need.

Looking for setup steps? This page documents the class itself. For installing the starter plugin, replacing placeholders, and registering a column, see How to create my own column.


Class hierarchy

There are three factory classes, layered from general to specific. Each layer adds methods on top of the one above it.

  • AC\Column\ColumnFactory  is the free base class. It defines the core methods: create()get_column_type()get_label()get_description()get_settings() , and get_formatters() .
    • ACP\Column\ColumnFactory  extends it and adds the Pro feature methods: get_editing()get_sorting()get_search()get_export()get_conditional_format() , and get_metric() .
      • ACP\Column\AdvancedColumnFactory  extends that and provides a finished create() . This is the class you normally extend.

Because AdvancedColumnFactory already implements create(), you never write that method yourself. You only override the methods whose behavior you want to change, and leave the rest at their defaults.



class Column extends ACP\Column\AdvancedColumnFactory
{
    public function get_label(): string
    {
        return __('My Custom Field', 'your-textdomain');
    }

    public function get_column_type(): string
    {
        return 'ac-my_custom_field';
    }

    protected function get_formatters(AC\Setting\Config $config): AC\FormatterCollection
    {
        return new AC\FormatterCollection([
            new AC\Formatter\Meta(AC\MetaType::create_post_meta(), 'my_custom_field_key'),
        ]);
    }
}

Core methods

Defined on AC\Column\ColumnFactory  and available in every subclass.

get_column_type()

Returns string. Required. A unique identifier for the column type, for example ac-my_custom_field. Use a single token of letters, digits, underscores, or hyphens, and add a prefix so it cannot collide with built-in or other third-party column types. This value is stored in the column configuration, so changing it later detaches existing columns of this type.

get_label()

Returns string. Required. The human-readable name shown in the column-type dropdown and used as the default column heading. Wrap it in a translation function so it can be localized.

create(Config $config)

Returns AC\Column . Builds the column object. Abstract on the base class. AdvancedColumnFactory  implements it for you by merging the default settings, your get_settings(), and the feature settings, then assembling the column. Override this only if you extend the base class directly and need full control over construction.

get_formatters(Config $config)

Returns AC\FormatterCollection. The formatters that turn a raw row value (most of the time an ID) into what the user sees in the cell. Formatters run in order, each receiving the previous one's output, so you can chain them. For example, read a value and then wrap it in a link. Use a built-in formatter such as AC\Formatter\Meta, or write your own by implementing the AC\Formatter  interface.

get_settings(Config $config)

Returns AC\Setting\ComponentCollection. Extra settings fields are shown for the column in the Admin Columns settings screen, on top of the defaults every column gets. Each setting can also contribute its own formatters.

get_description()

Returns ?string. Optional helper text shown beneath the column type when a user is choosing it. Returns null  by default.

Feature methods (Pro)

Defined on ACP\Column\ColumnFactory. Each one enables a Pro feature for the column. Every method receives the column Config, so you can vary behavior by the column's own settings.

get_editing(Config $config)

Returns?ACP\Editing\Service, or null for no editing (the default). Return an editing service to allow inline and bulk editing of the column's value. Use a built-in service such as ACP\Editing\Service\Post\Meta, or implement ACP\Editing\Service for full control over how the value is read, which input is shown, and how it is saved.

protected function get_editing(AC\Setting\Config $config): ?ACP\Editing\Service
{
    return new ACP\Editing\Service\Post\Meta(
        'my_custom_field_key',
        new ACP\Editing\View\Text()
    );
}

get_sorting(Config $config)

Returns ?ACP\Sorting\Model\QueryBindings , or null for not sortable (the default). Return a sorting model to make the column sortable. Built-in models such as ACP\Sorting\Model\Post\Meta  cover common cases, or implement QueryBindings to supply your own SQL JOIN and ORDER BY.

get_search(Config $config)

Returns ?ACP\Search\Comparison, or null for not filterable (the default). Return a comparison to enable smart filtering. Built-in comparisons such as ACP\Search\Comparison\Meta\Text  handle the usual field types, or extend ACP\Search\Comparison  to define your own operators and query bindings.

get_export(Config $config)

Returns?AC\FormatterCollection . Controls the value written when exporting to CSV. Unlike the other feature methods, this one is enabled by default: it reuses get_formatters()  with sanitizing applied, so a column exports sensibly without any extra code. Override it only when the export value should differ from what is shown in the cell.

get_conditional_format(Config $config)

Returns?ACP\ConditionalFormat\FormattableConfig, or null by default. Return a formattable config to let users style cells based on their value, for example, coloring a number red when it falls below a threshold.

get_metric(Config $config)

Returns ?ACP\Metric\Config, or null by default. Return a metric config to show an aggregate value for the column, such as a sum or average, in the table footer.

The Config argument

Every overridable method receives an AC\Setting\Config  object. It holds the saved configuration for this specific column instance, including its name and any values from the fields returned by get_settings(). Read from it when a method's behavior should depend on how the user configured the column, for example a meta key chosen in a settings field.

It exposes four methods:

  • has($key)  returns whether a value is set.
  • get($key, $default = null)  returns a value, or the default when it is missing.
  • all()  returns the full configuration as an array.

For example, read a meta key the user picked in a settings field and fall back to a default:

protected function get_formatters(AC\Setting\Config $config): AC\FormatterCollection
{
    $meta_key = (string) $config->get('meta_key', 'my_custom_field_key');

    return new AC\FormatterCollection([
        new AC\Formatter\Meta(AC\MetaType::create_post_meta(), $meta_key),
    ]);
}

Still need help? Contact Us Contact Us