Posted On: February 15, 2017

I recently started using ACF to create and mange my custom fields for my posts and custom post types. One of the things I quickly needed to figure out was how I could keep all the custom fields in sync across my environments. It was also important for other developers working on the project to automatically have any new fields when they pulled the latest code. Thankfully, ACF has built in export functionality that makes this simple.

When logged into the admin, navigate to "Custom Fields > Export". Here you will find a list of all of your custom fields with options to export to XML or PHP.

Select which fields you want to export, and click "Export to PHP". This will generate the code needed with instructions for adding this code to your theme. I actually prefer to add this file to a custom plugin that I typically include with all Wordpress sites. I feel that new fields should not be tied to the theme (presentation) layer but rather to the plugin (functionality) layer. If you switch themes, you should still have the same fields, but the themes may display these fields differently.

The Plugin Route

Inside a custom plugin, create a new directory named "acf". Inside this directory, add a new file called "acf.php" and paste the generated code inside the file.

if(function_exists("register_field_group"))
{
  register_field_group(array (
    'id' => 'acf_events',
    'title' => 'Events',
    'fields' => array (
      array (
        'key' => 'field_32eadd836af73',
        'label' => 'Date',
        'name' => 'date',
        'type' => 'date_picker',
        'date_format' => 'yymmdd',
        'display_format' => 'dd/mm/yy',
        'first_day' => 0,
      ),
    ),
    'location' => array (
      array (
        array (
          'param' => 'post_type',
          'operator' => '==',
          'value' => 'event',
          'order_no' => 0,
          'group_no' => 0,
        ),
      ),
    ),
    'options' => array (
      'position' => 'normal',
      'layout' => 'no_box',
      'hide_on_screen' => array (
      ),
    ),
    'menu_order' => 0,
  ));
}

Now you will need to require this file in your plugin file.

require_once( plugin_dir_path( __FILE__ ) . 'acf/acf.php' );

Other Issues

You will now see duplicate fields, so you will need to delete the custom field definition from the "Custom Fields" page.

Click "Trash" to remove the field definition. This will remove the duplicate field. You can now commit your changes and push them to your repo.

If you need to add a new field after you have already exported your fields to php, you will have to create a new custom field definition with only the new field. When you go to export the definition this time, you will only copy the new field array and paste that into your existing acf.php file under the "fields" key for the corresponding post type as identified in the "location" key.

Improvements?

This way has worked well for me. It is a little annoying that I have to merge the exported content when adding new fields and then delete the custom field definition from the admin, but I'll take it. Do you have a better way? How do you manage ACF under version control?

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.