Skip to content

Instantly share code, notes, and snippets.

@mgburns
Created September 1, 2015 13:14
Show Gist options
  • Save mgburns/23ffdbd20c580c715c47 to your computer and use it in GitHub Desktop.
Save mgburns/23ffdbd20c580c715c47 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' );
@nicoladj77
Copy link

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

@broskees
Copy link

broskees commented Apr 4, 2018

@ShpendBerisha
Copy link

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',
),

@zenideas
Copy link

The action acf/init works with Pro version of ACF. For basic version you have to use acf/register_fields to register your custom fields.

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

@thewebprojects
Copy link

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