wp-lemon Docs
ACF fields
ACF fields
Although it's possible to add or modify ACF fields using the build in UI method, we highly recommend adding the fields using PHP configurations. We find this method more robust, especially in larger websites. If you want to see all the different field types that exist and their default attributes you can take a look right here.
To add or modify ACF fields using PHP configurations you need a field group. Within wp-lemon some post types will already have an existing field group. In the following documentation we will explain both how to modify/remove existing field groups and how to create new groups from scratch.
Modifying existing fieldgroups
Because these post types will have a fieldgroup by default you don't have to create it yourself.
By navigating to the library/hooks.php
file in your child theme, you can use filters to add, modify or remove fields in an existing fieldgroup.
below we've listed all the existing fieldgroups with the filter name you can use.
Fieldgroups | Fieldgroups filter name |
---|---|
Job | wp-lemon/filter/model/acf-fields/job |
Person | wp-lemon/filter/model/acf-fields/person |
Menu | wp-lemon/filter/model/acf-fields/menu |
Adding ACF fields
/** * Fields * * @package WordPress * @subpackage WP_Lemon\Child */
namespace WP_Lemon\Child\Models;
use StoutLogic\AcfBuilder\FieldsBuilder;add_filter( 'wp-lemon/filter/model/acf-fields/job', function (FieldsBuilder $job) { $job ->addText('job_location'); return $job; },);
Remove ACF Fields
In the same way we can remove an existing ACF field by using the build-in function that ACF provides like so:
/** * Fields * * @package WordPress * @subpackage WP_Lemon\Child */
namespace WP_Lemon\Child\Models;
add_filter( 'wp-lemon/filter/model/acf-fields/job', function (FieldsBuilder $job) { $job ->removeField('salary'); return $job; },);
Remove all ACF Fields at once
In case you want to remove the whole specific field group. you can return false
in the filter.
You can use this option to completely build a field group from the ground up.
add_filter( 'wp-lemon/filter/model/acf-fields/job', function ($job) { return false; },);
In the examples above we showed you the different options separately, but when you're modifying a field group you can remove and add multiple ACF fields all within the same filter. You do this by chaining the methods together like so:
/** * Fields * * @package WordPress * @subpackage WP_Lemon\Child */
namespace WP_Lemon\Child\Models;
use StoutLogic\AcfBuilder\FieldsBuilder;
add_filter( 'wp-lemon/filter/model/acf-fields/job', function (FieldsBuilder $job) { $job ->removeField('salary') ->addText('job_location'); return $job; },);
Creating new fieldgroups
When adding ACF fields to a post type that doesn't already have a fieldgroup you can start by creating a file in library/models
folder of your child theme.
For example fields-news.php
. Inside this file:
<?php
/** * Adds extra fields to news. * * @package WordPress * @subpackage WP_Lemon */
namespace WP_Lemon\Child\Models;
use StoutLogic\AcfBuilder\FieldsBuilder;
$news = new FieldsBuilder( 'news', [ 'title' => __('Additional news fields', 'wp-lemon-child'), 'style' => 'seamless', ]);
add_action('acf/init', function () use ($news) { $news ->addText('label', [ 'label' => __('Label', 'wp-lemon-child'), 'required' => 1, ]) ->setLocation('post_type', '==', 'news');
acf_add_local_field_group($news->build());});
In case of a custom block
When you've build your own ACF block and want to add ACF fields to it, you'll have to go trough a similar process. you can find more information on this here.
Edit this page on GitHub