pxlrbt/acf-configurator

This package is abandoned and no longer maintained. No replacement package was suggested.

Wrapper for easy local ACF configuration in php.

0.3.2 2021-01-23 12:37 UTC

This package is auto-updated.

Last update: 2023-01-31 00:37:00 UTC


README

A wrapper for easy local Advanced Custom Fields configuration in php.

ACF Configurator comes into play when you want to define Advanced Custom Fields groups in PHP and not via database. Since ACFs standard way of configuration is a long multidimensional array, it gets confusing very fast. ACF Configurator provides an easy, intuive way to configure ACF groups and fields. It uses a short, OOP oriented syntax for configuration.

Why not use the Advanced Custom Fields GUI?

The Advanced Custom Fields plugin provides a nice interface which is convenient for managing groups and fields in WordPress. Even clients can easily edit field with little knowledge. But configuration via PHP has some advantages:

  • No loading from database. Configuration is not stored in database anymore and does not need to be fetched every time the page loads.
  • Couple code and configuration. Configuration does not belong into a database. Custom fields and the themes codebase are usually coupled together tightly. Changing fields without updating the code will probalby break things.
  • Easier migration. If a theme needs an update you usually test it in an development environment first. Transferring all the changes you made to your live site before deploying your new code, can break things, or at least is really annoying.

Installation

Install this package via Composer:

composer require pxlrbt/acf-confgurator

Usage (Example)

Just import ACF Configurator and start configuration. Group class takes care of registration.

<?php

use pxlrbt\AcfConfigurator\FieldGroup;
use pxlrbt\AcfConfigurator\Condition\Condition;
use pxlrbt\AcfConfigurator\Location\Location;
use pxlrbt\AcfConfigurator\Fields\Email;
use pxlrbt\AcfConfigurator\Fields\Text;
use pxlrbt\AcfConfigurator\Fields\TrueFalse;
use pxlrbt\AcfConfigurator\Fields\Repeater;
use pxlrbt\AcfConfigurator\Fields\Image;

FieldGroup::make('Test group', 'test')
    ->location(function($condition) {
        $condition->if(Location::$PARAM_POST_TEMPLATE, Location::$OPERATOR_EQUALS, 'template.php')
            ->andIf(Location::$PARAM_POST_TYPE, Location::$OPERATOR_EQUALS, 'page');
    })
    ->fields([
        Text::make('Text field', 'text')
            ->placeholder('Placeholder')
            ->required(true),

        TrueFalse::make('Show email?', 'show_email')
            ->select2(true),

        Email::make('Email address', 'email')
            ->condition(function($condition) {
                $condition->if('field_show_email', Condition::$OPERATOR_EQUALS, true);
            })
            ->required(true)
            ->placeholder('your-email@example.com'),

        Repeater::make('Items', 'items')
            ->fields([
                Textfield::make('Description', 'description'),
                Image::make('Image', 'item__image')
                    ->returnFormat(Image::$FORMAT_ID)
            ])
    ])
    ->hide(FieldGroup::$HIDE_EDITOR)
    ->hide(FieldGroup::$HIDE_TRACKBACKS);

Alternatives

This plugin doesn't fit your needs? Here are some alternatives:

Contributing

Contribution to this project is appreciated. Feel free to add non-standard field types or suggest improvements.