Use Object Oriented Programming to create fields groups for ACF.
composer require pjehan/acfoop
Create a new class extending the abstract Container class:
use Pjehan\Acfoop\Container;
class Post extends Container
{
}
Override the addCustomFields
method to add your fields groups.
This method is automatically called when the hook 'acf/init' is fired.
use Pjehan\Acfoop\Container;
use Pjehan\Acfoop\FieldGroup;
class Post extends Container
{
public const GROUP_SLUG = 'group_post';
public function addCustomFields(): void
{
$group = new FieldGroup(self::GROUP_SLUG, 'My Group');
}
}
You can also set the group's location rules:
use Pjehan\Acfoop\Container;
use Pjehan\Acfoop\FieldGroup;
use Pjehan\Acfoop\Location;
class Post extends Container
{
public const GROUP_SLUG = 'group_post';
public function addCustomFields(): void
{
$group = new FieldGroup(self::GROUP_SLUG, 'My Group');
$group->addLocation(new Location('post_type', 'post'));
}
}
Then, you can instantiate the class and the addCustomFields
method will be called automatically:
new Post();
Then, you can add fields to the group. You can use one of the following Fields classes:
use Pjehan\Acfoop\Fields\Text;
$text = new Text('text_field', 'Text Field');
$group->addField($text);