PHP code example of anthonypauwels / wp-acf-builder
1. Go to this page and download the library: Download anthonypauwels/wp-acf-builder library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
anthonypauwels / wp-acf-builder example snippets
$home_group = Builder::pageTemplate('templates/home.php', 'Home Page', function ( Location $group ) {
$group->group('Header', function ( Group $group ) {
$group->text('Title');
$group->text('Tagline');
$group->text('Button');
$group->group('Background', function ( Group $group ) {
$group->text('Video');
$group->image('Image')->returnUrl();
} );
} );
$group->group('Discover', function ( Group $group ) {
$group->repeater('Subsection', function ( Repeater $group ) {
$group->text('Title');
$group->text('Subtitle');
$group->wysiwyg('Content');
$group->image('Image')->returnUrl();
$group->group('Button', function ( Group $group ) {
$group->text('Title');
$group->pageLink('Page')->postType('page');
} );
} );
} );
$group->group('Blog', function ( Group $group ) {
$group->text('Title');
$group->text('Subtitle');
$group->group('Button', function ( Group $group ) {
$group->text('Title');
$group->pageLink('Page')->postType('page');
} );
} );
} )->hideAll();
// group is stored into `$home_group` but not already build. You can call the method $home_group->build() to build the group into ACF.
// Create fields for a post type
Builder::postType('post', 'Post Field', function ( Location $group ) {
// ...
$group->postType('another_post_type', 'or'); // second parameter is optional and can be `or` or `and`
} );
// Create fields for an options page
Builder::optionsPage('Options', function ( Location $group ) {
$group->image('Logo')->returnUrl();
$group->text('Copyright');
} );
$home_group->build(); // see example above
$about_group->build();
$contact_group->build();
Builder::build( function () {
Builder::pageTemplate('templates/home.php', 'Home Page', function ( Location $group ) {
// ...
} );
// work also with optionsPage and postType methods
} );
## AcfServiceProvider.php, by example
Builder::build( function () {
$banner_group = Builder::group('Banner', function (Group $group) {
$group->text('Title');
$group->text('Subtitle');
$group->group('Button', function ( Group $group ) {
$group->text('Title');
$group->pageLink('Page')->postType('page');
} );
$group->group('Background', function ( Group $group ) {
$group->text('Video');
$group->image('Image')->returnUrl();
} );
} );
Builder::pageTemplate('templates/home.php', 'Home Page', function ( Location $group ) use ( $banner_group ) {
// ...
$group->addField( $banner_group );
// ...
} );
Builder::pageTemplate('templates/contact.php', 'Contact Page', function ( Location $group ) use ( $banner_group ) {
// ...
$group->addField( $banner_group );
// ...
} );
Builder::pageTemplate('home.php', 'Home Page', function ( Location $group ) {
$group->text('Title')->onValue( function ( $field ) {
$field['value'] = strtoupper( $field['value'] );
return $field;
} ); // end of text field Title
} );
Builder::pageTemplate('home.php', 'Home Page', function ( Location $group ) {
$group->repeater( 'List', function ( Repeater $repeater ) {
$repeater->text('Label' );
$repeater->text('Value' );
} )->onValue( function ( $value ) {
// if the value is FALSE or NULL it means the field has never been updated
// we don't want to change fields that have already been edited
if ( !$value ) {
// create a bi dimensional array
return array_map( function ( $label ) {
return [
'_acf_field_group_home_page_fields_list_label' => $label,
'_acf_field_group_home_page_fields_list_value' => '',
];
}, ['First Label', 'Second Label', 'Third Label'] );
}
return $value;
} ); // end of repeater List
} );
// ...
function ( Group $group ) {
$group->text('Sub Title')->param('wpml_cf_preferences', 2 ); // This field will be translated
$group->text('Sub Title')->params( ['wpml_cf_preferences' => 2 ] ); // you can pass array of parameters with `params` method
} )
// ...
Builder::config( [
// define a config value for all fields, like using `param` method
Builder::all => [
'wpml_cf_preferences' => 2,
],
// define config for a field using the building methods
Builder::text => Builder::text()->placeholder('Put some content here'),
// methods can be chained
Builder::wysiwyg => Builder::wysiwyg()->visualOnly()->basicToolbar()->lder::googleMap()->api('your_api_key_here', 'your_client_ID_here'),
// works with repeater, group, location, flexible and layout too
Builder::repeater => Builder::repeater()->max( 10 )->param( 'wpml_cf_preferences', 0 ),
] );
Builder::build( function () { } );
Builder::namespace( 'templates', function () { } );
Builder::fieldsGroup( 'Group label', function ( Group $group ) { } );
Builder::repeaterGroup( 'Repeater label', function ( Repeater $repeater ) { } );
Builder::flexibleContent( 'Flexible Content label', function ( Flexible $flexible ) { } );
Builder::locationGroup( 'Group Location label', function ( Location $location ) { } );
Builder::postType( 'Location Post Type', function ( Location $location ) { } );
Builder::pageTemplate( 'Location Page Template', function ( Location $location ) { } );
Builder::optionsPage( 'Location Options Page', function ( Location $location ) { } );
$group->accordion(/* ... */)->open(); // pass `false` to close
$group->accordion(/* ... */)->close();
$group->accordion(/* ... */)->multiExpand(); // pass `false` to do not multi expand
$group->accordion(/* ... */)->dontMultiExpand();
$group->boolean(/* ... */)->message('Here is an informative message');
$group->googleMap(/* ... */)->latitude(-37.81411);
$group->googleMap(/* ... */)->longitude(144.96328);
$group->googleMap(/* ... */)->coordinates(-37.81411, 144.96328);
$group->googleMap(/* ... */)->zoom(14);
$group->googleMap(/* ... */)->height(400);
// set your Google Map api key and client ID here. Works for all GoogleMapField, you can set it globally via the config
$group->googleMap(/* ... */)->api("your_api_key", "your_client_id");
$group->url(/* ... */)->placeholder('This is placeholder');
$group->user(/* ... */)->nullable(); // pass `false` to set not nullable
$group->user(/* ... */)->notNullable();
$group->user(/* ... */)->multiple(); // pass `false` to set not multiple
$group->user(/* ... */)->notMultiple();
$group->user(/* ... */)->roles(['administrator', 'editor']); // array of roles
$group->wysiwyg(/* ... */)->tabs('all'); // all | visual | text
$group->wysiwyg(/* ... */)->allTabs();
$group->wysiwyg(/* ... */)->visualOnly();
$group->wysiwyg(/* ... */)->textOnly();
$group->wysiwyg(/* ... */)->toolbar('full'); // full | basic
$group->wysiwyg(/* ... */)->fullToolbar();
$group->wysiwyg(/* ... */)->basicToolbar();
$group->wysiwyg(/* ... */)->showMediaButton(); // pass `false` to hide media button
$group->wysiwyg(/* ... */)->hideMediaButton();