Default ACF Flexible Content Fields

I had a recent request to set up some default Advance Custom Fields Flexible Content Fields for new posts. We often use FCF blocks for a lot of our page creation to allow for greater customization and flexibility. While this might seem like limiting some of that flexibility it comes with the benefit of streamlining things for your client.

Limiting the filter to only run on certain post types I can set up some default blocks and settings to make things easier and quicker to get up and running. This makes it easier on the client, and makes them happy. And a happy client, is a happy coder.

The ACF filter we’ll use is acf/load_value/name={$field_name}, read more on the ACF Docs. You need to set the name as the name of your flexible content blocks field name.

In this example I name mine content_blocks

add_filter( 'acf/load_value/name=content_blocks', 'default_fields', 10, 3 );
function default_fields( $value, $post_id, $field ) {

  // Only add default content for new posts
  if ( $value !== null ) {
    return $value;
  }

  // Only add default content for certain post types
  if ( ! in_array(
		get_post_type( $post_id ),
		array(
			@TODO Post Types
		) ) ) {
		return $value;
	}

  // Set up your initial post, then get the field id's of the settings you want to set
  // They look like: field_5b4e57efd0660_field_5b4e63e479d81
  // Add them after the acf_fc_layout
  $value = array(
    array(
      'acf_fc_layout' => 'name_of_layout',
      'field_5b4e57efd0660_field_5b4e63e479d81' => 'Default Value'
    ),
    array(
      'acf_fc_layout' => 'another_layout',
    )
  );

  return $value;
}

First we check the $value and make sure it’s set to null. If not than the post has already been created and we need to return early.

Next I check against the post type. There are a variety of checks you could do to make sure you’re only added default blocks where you want. You could check against the post template, post type, or even specific post ID’s.

Once you’re done checking you can setup the $value variable. The variable is an array, made up of arrays for each block you want to create.

The first value key pair you’ll need to set is the acf_fc_layout. This corresponds to the name of your layout in the flexible content field.

Any other fields that you want to set by default you need to first list the field key, and then your default value.

Finding Field Keys

You can find the field keys you need for this array in a couple of ways. You can go into your screen settings of the field group and toggle on the Field Keys

You can also go to a post that you have already created and want to use as default and output the $value variable. To do so you could add the following to your above filter:

echo ‘<pre>’;print_r($value); echo ‘</pre>’

Do this before you return early since the post has already been created. Then you’ll see an output of all the field keys and easily match them to the settings you want to set up as default.

However I prefer to use Kint for my debugging. Then you only have to add:

d($value);

Wrapping Up

ACF is full of extra filters for doing pretty much anything you can imagine. Check out the docs and support forums if you ever get stuck.

Leave a Reply

Your email address will not be published. Required fields are marked *