Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example programmatic registration of Advanced Custom Fields fields
<?php
/**
* Example programmatic registration of Advanced Custom Fields fields
*
* @see http://www.advancedcustomfields.com/resources/register-fields-via-php/
*/
function register_custom_acf_fields() {
if ( function_exists( 'acf_add_local_field_group' ) ) {
// ACF Group: People
acf_add_local_field_group( array (
'key' => 'group_person_details',
'title' => 'Person Details',
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'person',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
) );
// First name
acf_add_local_field( array(
'key' => 'field_first_name',
'label' => 'First Name',
'name' => 'first_name',
'type' => 'text',
'parent' => 'group_person_details',
'instructions' => '',
'required' => 1,
) );
// Middle name
acf_add_local_field( array(
'key' => 'field_middle_name',
'label' => 'Middle Name',
'name' => 'middle_name',
'type' => 'text',
'parent' => 'group_person_details',
'instructions' => '',
'required' => 0,
) );
// Last name
acf_add_local_field( array(
'key' => 'field_last_name',
'label' => 'Last Name',
'name' => 'last_name',
'type' => 'text',
'parent' => 'group_person_details',
'instructions' => '',
'required' => 1,
) );
// Image
acf_add_local_field( array(
'key' => 'field_image',
'label' => 'Image',
'name' => 'image',
'type' => 'image',
'parent' => 'group_person_details',
'instructions' => '',
'required' => 0,
'return_format' => 'array',
'preview_size' => 'thumbnail',
'library' => 'all',
'min_width' => 0,
'min_height' => 0,
'max_width' => 0,
'max_height' => 0,
) );
// Office
acf_add_local_field( array(
'key' => 'field_office',
'label' => 'Office',
'name' => 'office',
'type' => 'text',
'parent' => 'group_person_details',
'instructions' => '',
'required' => 0,
) );
// E-mail
acf_add_local_field( array(
'key' => 'field_email',
'label' => 'E-mail',
'name' => 'email',
'type' => 'email',
'parent' => 'group_person_details',
'instructions' => '',
'required' => 0,
) );
// Office Phone
acf_add_local_field( array(
'key' => 'field_phone',
'label' => 'Phone',
'name' => 'phone',
'type' => 'text',
'parent' => 'group_person_details',
'instructions' => '',
'required' => 0,
) );
// Affiliation
acf_add_local_field( array(
'key' => 'field_affiliation',
'label' => 'Affiliation',
'name' => 'affiliation',
'type' => 'taxonomy',
'parent' => 'group_person_details',
'instructions' => '',
'required' => 0,
'taxonomy' => 'affiliation',
'field_type' => 'multi_select', // UI (checkbox, multi-select, select, radio)
'allow_null' => 0, // Can select a blank value
'load_save_terms' => 1, // Persist using term relationships table
'return_format' => 'object', // or 'object'
'add_term' => 0, // Can the user add new terms?
) );
}
}
add_action( 'init', 'register_custom_acf_fields' );
@terriann

This comment has been minimized.

Copy link

commented Mar 13, 2018

Great example! I have an updated clarification to add:

The latest recommended action to use is acf/init according to the ACF documentation.
ex. add_action( 'acf/init', 'register_custom_acf_fields' );

Add within an action
The functions above can be used in the root of the functions.php file or within the acf/init action. This action was added in ACF v5.2.7 and is recommended.

Source: https://www.advancedcustomfields.com/resources/register-fields-via-php/

@nicoladj77

This comment has been minimized.

Copy link

commented Mar 18, 2018

How do you add fields to different groups without redefining fields each time?

@broskees

This comment has been minimized.

@ShpendBerisha

This comment has been minimized.

Copy link

commented Jun 15, 2018

Im trying to add fields in my custom taxonomy using the following location but I can't figure it out other other fields for custom post types are working fine but in taxonomy maybe Im doing it wron do you have any idea?
```
array (
'param' => 'taxonomy',
'operator' => '==',
'value' => 'my_tax',
),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.