Created
September 1, 2015 13:14
-
-
Save mgburns/23ffdbd20c580c715c47 to your computer and use it in GitHub Desktop.
Example programmatic registration of Advanced Custom Fields fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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, | |
) ); | |
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' ); |
How do you add fields to different groups without redefining fields each time?
I just had the same question after looking at the docs. The answer is: you define the fields as variables first, then assign those field variables to the group. Consider the following...
// Define a field
$field1 = array(
'key' => 'field_1',
'label' => 'Title',
'name' => 'title',
'type' => 'text'
);
// Define another field
$field2 = array(
'key' => 'field_2',
'label' => 'Sub Title',
'name' => 'sub_title',
'type' => 'text'
);
// Add a field group
acf_add_local_field_group(array(
'key' => 'group_1',
'title' => 'My Group',
'fields' => array ($field1, $field2), // Assign those field vars
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'page',
),
),
),
));
I hope that helps folks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Function details https://docs.wpdebuglog.com/plugin/advanced-custom-fields/5.8.8/function/acf_add_local_field_group/