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.

3 Comments

Ben Palmer September 19, 2019 Reply

Really cool little trick, ive prepopulated them with a custom query but this is a great little thing to help clients with flexible content. It’s strange that the more they can do with the flexible fields these days, the more bare the site seems when you first present it to them. This should help ease them into it a big and reduce documentation.

Michelle McGinnis October 15, 2019 Reply

This is so great, thank you! Very useful.

Courtney August 10, 2021 Reply

Thank you so much for this! I have been mulling on doing something along these lines for a long time and am so glad for this help.

One note: I got a little confused by the “field_5b4e57efd0660_field_5b4e63e479d81” you had in there. In my case, my ID was like “field_5b577cef6ed7e” (only one field_XXXX not field_XXXX_field_XXXX). But, spitting out the value helped clarify what I needed in my situation.

Leave a Reply to Ben Palmer Cancel reply