Support

Account

Home Forums Add-ons Flexible Content Field Pre-Defined/Required Flexible Layouts

Helping

Pre-Defined/Required Flexible Layouts

  • Is there a way to have pre-defined/required layouts, that are already selected and not removable by the user?

    In my example, I will let the user create their own payment form. I’m wanting to always require the “Name & Address block”, as well as the “Credit Card block”. The user should still be able to move these blocks around within the other fields that he/she is going to create.

  • What version of ACF are you using. I’m assuming Pro (V5) since you’re using flex fields. When defining a layout you can specify a minimum and a maximum number of the layout that must/can be created. Setting them both to 1 will make them required.

    Adding them as defaults on a new post is a little bit more difficult, but it is possible. You can use an acf/load_value filter https://www.advancedcustomfields.com/resources/acfload_value/. You can test the current value and if it is exactly NULL this means that the value has not been created yet and it is a new post. You can then return a value for the flex field that includes a set of layouts to appear automatically on the new post. To see what you need to return, create an acf/load_value filter for the field and then output the current value. I don’t know the exact setup for a flex field value but you can always see what you need to return by looking at what’s already a value in any ACF field type.

    
    add_filter('acf/load_value/name=YOUR_FIELD_NAME', 'YOUR_FUNCTION_NAME', 10, 3);
    function YOUR_FUNCTION_NAME($value, $post_id, $field) {
      echo '<pre>'; print_r($value); echo '</pre>';
      return $value;
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Pre-Defined/Required Flexible Layouts’ is closed to new replies.