yard / acf-registrar
PHP package to register ACF Field Groups, Forms and Option Pages
Requires
- php: >=8.2
- vinkla/extended-acf: ^14
Requires (Dev)
- 10up/wp_mock: ^0.4.2
- pestphp/pest: ^2.36
- php-stubs/acf-pro-stubs: ^6.3
- phpstan/extension-installer: ^1.4
- szepeviktor/phpstan-wordpress: ^1.3
- yard/php-cs-fixer-rules: ^1.0
README
Features
- Register ACF Field groups
- Register ACF Forms
- Register ACF Option Pages
Installation
This package can be installed using composer
composer require yard/acf-registrar
Usage
To use this package in a standard WordPress plugin, you can use the Registrar to register hooks.
main file:
/** * Plugin Name: My Plugin */ require __DIR__ . '/vendor/autoload.php'; $fieldGroups$ = [ \Plugin\FieldGroupClass::class, \Plugin\AnotherFieldGroupClass::class, ]; $registrar = new \Yard\Acf\Registrar(); $registrar->addFieldGroups($fieldGroups); $registrar->addForm(\Plugin\FormClass::class); $registrar->addOptionPage(\Plugin\OptionPageClass::class); $registrar->register();
FieldGroup Usage
Extend Yard\Acf\Registar\FieldGroup to define a field group.
Add the class to config/acf-registrar in the field_groups key.
See Extended ACF for documentation about registering fields.
<?php declare(strict_types=1); namespace App\FieldGroups; use Extended\ACF\Fields\Text; use Extended\ACF\Location; use Yard\Acf\Registrar\FieldGroup; class Person extends FieldGroup { public function getTitle(): string { return 'Instellingen Persoon'; } public function getFields(): array { return [ Text::make('Naam', 'name') ->instructions('De naam van de persoon.') ->required(true) ->placeholder('Voer de naam in'), ]; } public function getLocation(): array { return [ Location::where('post_type', '==', 'person'), ]; } }
Forms Usage
Extend Yard\Acf\Registrar\Forms to define a front-end form.
Add the class to config/acf-registrar under the forms key.
getId() is required to define the form id, additional methods can be used to overwrite the given defaults.
<?php declare(strict_types=1); namespace App\Forms; use Yard\Acf\Registrar\Form; class Person extends Form { public function getId(): string { return 'form-id'; } public function getFields(): array { return [ Person::SOME_FIELD, Person::MORE_FIELDS ]; } }
Option page Usage
Extend Yard\Acf\Registrar\OptionPage to define a option page.
Add the class to config/acf-registrar under the option_pages key.
getPageTitle() is required to define the option page dashboard name and page title, additional methods can be used to overwrite the given defaults.
<?php declare(strict_types=1); namespace App\OptionPages; use Yard\Acf\Registrar\OptionPage; class PersonOptions extends OptionPage { public function getPageTitle(): string { return 'Option page example'; } public function getCapability(): string { return 'edit_posts'; } }
Option pages require the registration of 1 or more fieldgroups that are linked to the option page via:
<?php public function getLocation(): array { return [ Location::where('option_page', '==', 'acf-options-option-page-example'), ]; }