Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Oscar-Abad-Folgueira/cbee0d7426bc2d82335d8562cbb4b11c to your computer and use it in GitHub Desktop.
Save Oscar-Abad-Folgueira/cbee0d7426bc2d82335d8562cbb4b11c to your computer and use it in GitHub Desktop.
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' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment