Register advanced custom fields with object-oriented PHP
Register advanced custom fields with object-oriented PHP
Register advanced custom fields with object-oriented PHP.
Extended ACF provides an object-oriented API to register groups and fields with ACF. If you register fields in your theme, you can safely rely on version control when working with other developers. Oh, you don't have to worry about unique field keys.
Require this package, with Composer, in the root directory of your project.
composer require vinkla/extended-acfTo install the Advanced Custom Fields Pro plugin, download and place it in either the plugins or mu-plugins directory. After that, activate the plugin in the WordPress dashboard.
Learn more about installing ACF PRO using Composer.
To register a new field group, use the register_extended_field_group() function. This extends the default register_field_group() function from the ACF plugin. The key value is appended to field groups and fields. Here's an example of a field group.
use Extended\ACF\Fields\Image;
use Extended\ACF\Fields\Text;
use Extended\ACF\Location;
add_action('acf/include_fields', function() {
register_extended_field_group([
'title' => 'About',
'fields' => [
Image::make('Image'),
Text::make('Title'),
],
'location' => [
Location::where('post_type', 'page')
],
]);
});For detailed information on field group settings, please consult the official ACF documentation. You can also find more examples in the examples directory.
All fields, except the clone field, have a corresponding class. Each field needs a label. If no name is specified, the label will be used as the name in snake_case. The name can only contain alphanumeric characters and underscores.
use Extended\ACF\Fields\Text;
Text::make('Title', 'heading')
->helperText('Add the text value')
->required()Most fields have the methods default, required, and wrapper. The basic fields also have the methods prepend, append, placeholder, readOnly, and disabled. Please also check the non-standard methods mentioned in the non-standards section.
Email - The email field creates a simple email input.
use Extended\ACF\Fields\Email;
Email::make('Email')
->helperText('Add the employees email address.')
->required()Number - The number field creates a simple number input.
use Extended\ACF\Fields\Number;
Number::make('Age')
->helperText('Add the employees age.')
->min(18)
->max(65)
->required()Password - The password field creates a simple password input.
use Extended\ACF\Fields\Password;
Password::make('Password')
->helperText('Add the employees secret pwned password.')
->required()Range - The range field provides an interactive experience for selecting a numerical value.
use Extended\ACF\Fields\Range;
Range::make('Rate')
->helperText('Add the employees completion rate.')
->min(0)
->max(100)
->step(10)
->required()Text - The text field creates a simple text input.
use Extended\ACF\Fields\Text;
Text::make('Name')
->helperText('Add the employees name.')
->maxLength(100)
->required()Textarea - The textarea field creates a simple textarea.
use Extended\ACF\Fields\Textarea;
Textarea::make('Biography')
->helperText('Add the employees biography.')
->newLines('br') // br, wpautop
->maxLength(2000)
->rows(10)
->required()URL - The URL field creates a simple uRL input.
use Extended\ACF\Fields\URL;
URL::make('Website')
->helperText('Add the employees website link.')
->required()File - The file field allows a file to be uploaded and selected.
use Extended\ACF\Fields\File;
File::make('Resturant Menu', 'menu')
->helperText('Add the menu **pdf** file.')
->acceptedFileTypes(['pdf'])
->library('all') // all, uploadedTo
->minSize('400 KB')
->maxSize(5) // MB if entered as int
->format('array') // id, url, array (default)
->required()Gallery - The gallery field provides a simple and intuitive interface for managing a collection of images.
use Extended\ACF\Fields\Gallery;
Gallery::make('Images')
->helperText('Add the gallery images.')
->acceptedFileTypes(['jpg', 'jpeg', 'png'])
->minHeight(500)
->maxHeight(1400)
->minWidth(1000)
->maxWidth(2000)
->minFiles(1)
->maxFiles(6)
->minSize('400 KB')
->maxSize(5) // MB if entered as int
->library('all') // all, uploadedTo
->format('array') // id, url, array (default)
->previewSize('medium') // thumbnail, medium, large
->prependFiles()
->required()Image - The image field allows an image to be uploaded and selected.
use Extended\ACF\Fields\Image;
Image::make('Background Image')
->helperText('Add an image in at least 12000x100px and only in the formats **jpg**, **jpeg** or **png**.')
->acceptedFileTypes(['jpg', 'jpeg', 'png'])
->minHeight(500)
->maxHeight(1400)
->minWidth(1000)
->maxWidth(2000)
->minSize('400 KB')
->maxSize(5) // MB if entered as int
->library('all') // all, uploadedTo
->format('array') // id, url, array (default)
->previewSize('medium') // thumbnail, medium, large
->required()Oembed - The oEmbed field allows an easy way to embed videos, images, tweets, audio, and other content.
use Extended\ACF\Fields\Oembed;
Oembed::make('Tweet')
->helperText('Add a tweet from Twitter.')
->required()WYSIWYG - The WYSIWYG field creates a full WordPress tinyMCE content editor.
use Extended\ACF\Fields\WYSIWYGEditor;
WYSIWYGEditor::make('Content')
->helperText('Add the text content.')
->tabs('visual') // all, text, visual (default)
->toolbar(['bold', 'italic', 'link']) // aligncenter, alignleft, alignright, blockquote, bold, bullist, charmap, forecolor, formatselect, fullscreen, hr, indent, italic, link, numlist, outdent, pastetext, redo, removeformat, spellchecker, strikethrough, underline, undo, wp_adv, wp_help, wp_more
->disableMediaUpload()
->lazyLoad()
->required()Button Group - The button group field creates a list of radio buttons.
use Extended\ACF\Fields\ButtonGroup;
ButtonGroup::make('Color')
->helperText('Select the box shadow color.')
->choices(['Forest Green', 'Sky Blue']) // ['forest_green' => 'Forest Green', 'sky_blue' => 'Sky Blue']
->default('forest_green')
->format('value') // array, label, value (default)
->required()Checkbox - The checkbox field creates a list of tick-able inputs.
use Extended\ACF\Fields\Checkbox;
Checkbox::make('Color')
->helperText('Select the border color.')
->choices(['Forest Green', 'Sky Blue']) // ['forest_green' => 'Forest Green', 'sky_blue' => 'Sky Blue']
->default('forest_green')
->format('value') // array, label, value (default)
->layout('horizontal') // vertical, horizontal
->create(save: true) // allow custom values, save them to the choices
->toggle() // add a toggle all checkbox
->required()Radio Button - The radio button field creates a list of select-able inputs.
use Extended\ACF\Fields\RadioButton;
RadioButton::make('Color')
->helperText('Select the text color.')
->choices(['Forest Green', 'Sky Blue']) // ['forest_green' => 'Forest Green', 'sky_blue' => 'Sky Blue']
->default('forest_green')
->format('value') // array, label, value (default)
->create(save: true) // add an other choice, save custom values to the choices
->required()Select - The select field creates a drop down select or multiple select input.
use Extended\ACF\Fields\Select;
Select::make('Color')
->helperText('Select the background color.')
->choices(['Forest Green', 'Sky Blue']) // ['forest_green' => 'Forest Green', 'sky_blue' => 'Sky Blue']
->default('forest_green')
->format('value') // array, label, value (default)
->multiple()
->nullable()
->stylized() // stylized checkbox using select2
->lazyLoad() // use AJAX to lazy load choices
->create(save: true) // allow creation of new options, save them to the choices
->required()True False - The true / false field allows you to select a value that is either 1 or 0.
use Extended\ACF\Fields\TrueFalse;
TrueFalse::make('Social Media', 'display_social_media')
->helperText('Select whether to display social media links or not.')
->default(false)
->stylized(on: 'Yes', off: 'No') // optional on and off text labels
->required()Link - The link field provides a simple way to select or define a link (url, title, target).
use Extended\ACF\Fields\Link;
Link::make('Read More Link')
->format('array') // url, array (default)
->required()Page Link - The page link field allows the selection of 1 or more posts, pages or custom post types.
use Extended\ACF\Fields\PageLink;
PageLink::make('Contact Link')
->postTypes(['contact'])
->postStatus(['publish']) // draft, future, pending, private, publish
->taxonomies(['category:city'])
->disableArchives()
->nullable()
->multiple()
->required()Post Object - The post object field creates a select field where the choices are your pages +
No open issues yet, or sync has not completed.