Download the PHP package anthonypauwels/wp-acf-builder without Composer
On this page you can find all versions of the php package anthonypauwels/wp-acf-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download anthonypauwels/wp-acf-builder
More information about anthonypauwels/wp-acf-builder
Files in anthonypauwels/wp-acf-builder
Package wp-acf-builder
Short Description An Advanced Custom Field configuration builder using Closure and helpers methods
License MIT
Informations about the package wp-acf-builder
WP ACF Builder
Create ACF fields with ease using a fluent API.
Requiring WordPress with Advanced Custom Fields Pro.
Installation
Require this package with composer.
Usage
Basic
The main Builder class provides shortcut methods to help you create your fields. Here is an example of creating fields for your home page template :
Builder class provides methods to build fields group for Posts Types, Options Pages and Page Template. Location group also provides helpers methods to show them using conditional parameters :
Group instances that can exports sub_fields provides helpers methods to create them. It concerns Group, Layout, Location and Repeater. These methods help you to create subfields like TextField, TextareaField, ImageField, etc. Each class has its own helpers to configure themselves.
For example, you can define how the value of an image field must be returned using helpers methods like returnUrl()
, returnArray()
or returnId()
Groups with subfields helpers can also create subgroups using the same way. A Location can use repeater($name, $closure)
to create a repeater field on its own.
The helpers to create group (Repeater, Group, Layout) accept a closure with instance on parameter that provides the same API to configure fields.
Build
You can build your group separately using their method build
.
Builder class provides a static method build()
that accepts a closure as parameter. This method calls the closure first then create the acf/init
action. Every group created inside the closure will be internally marked for build and built inside the WordPress action.
Using that way, you do not need to call the build()
method of each group, Builder will make it for you.
If you want to export your fields into a separate file, you can also require it into the closure :
Namespace
The static method Builder::namespace
can be used to wrap many Builder::pageTemplate
calls. A namespace is simply a directory where your templates is stored.
Reuse
Here is an example of creating a banner group with predefined fields. When your group is created, you can push them into the fields list of another groups like Location, Repeater and Layout.
Filters
Fields and groups provides helpers methods for ACF filters. For example, you can create a filter where the returned value is transformed into capital case.
Or pre-populated a repeater fields :
For more information about ACF filters, check the Advanced Custom Fields documentation about Filters.
Raw parameters
If you want to set an ACF parameters not available on the package, you can use the param
method on fields. Imagine that you want to use WPML with your ACF fields,
you can set a value to wpml_cf_preferences
like this way :
Default configuration
Configure a default configuration to all fields can be long and daunting, so you can also use a config
method on the Builder
that accept an array as parameter.
The Builder has a bunch of new methods and constants to help to you to configure every field :
You must set default configuration before building fields because the configuration array are merged when build
method is called.
API documentation
WP ACF Builder provides a fluent API for each instances available.
- Builder
- Subfields
- Flexible Content
- Group
- Layout Group
- Location Group
- Repeater Group
- AbstractField
- AccordionField
- BooleanField
- ButtonGroupField
- CheckboxField
- ColorField
- DateField
- DateTimeField
- EmailField
- FileField
- GalleryField
- GoogleMapField
- ImageField
- LinkField
- MessageField
- NumberField
- OEmbedField
- PageLinkField
- PasswordField
- PostObjectField
- RadioField
- RangeField
- RelationshipField
- SelectField
- TabField
- TaxonomyField
- TextareaField
- TextField
- TimeField
- UserField
- WysiwygField
- Generic methods
Builder
Subfields
Class Group
, Location
, Layout
and Repeater
use Subfields
trait that provides the methods to create fields:
Flexible Content
Group
Layout Group
Location Group
Repeater Group
AbstractField
Every field inherit from AbstractField
class that provide generic API. Example below works with all fields :
AccordionField
BooleanField
ButtonGroupField
CheckboxField
ColorField
DateField
DateTimeField
EmailField
FileField
GalleryField
GoogleMapField
ImageField
LinkField
MessageField
NumberField
OEmbedField
PageLinkField
PasswordField
PostObjectField
RadioField
RangeField
RelationshipField
SelectField
TabField
TaxonomyField
TextareaField
TextField
TimeField
UrlField
UserField
WysiwygField
Generic methods
Both fields and groups can use these methods:
Requirement
PHP 8.0 or above