• Hi,

    I’m searching for a possibility to enable or disable certain layouts of a flexible content field, in order to give the user only a few options for generating his content.

    I tried to add a custom option to every layout being created. That did not work.

    It would also be important to save this setting for each field in the acf.json, so i think creating a custom option for each layout would be the right way to go.

    Any ideas how to achieve that?

    Thx for your help,

    Tom

Viewing 1 replies (of 1 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello yoyatom!

    Thanks for the feature request. In fact, there’s no option to disable layouts in the plugin yet. This setting would be interesting, but I think users would like to have large control over it. For example, instead of a site-wide disable, they would like to disable a layout on a specific post type, or for specific users.

    The scope of the setting would become kind of complicated if I wanted to implement such controls. A side-wide disable setting is kinda easy tho. I’ll think about it for the next update.

    In the meantime, here is a simple code implementation you can use in your functions.php file:

    
    add_filter('acf/prepare_field/type=flexible_content', 'my_flexible_content_layouts');
    function my_flexible_content_layouts($field){
        
        // Bail early if no layouts
        if(!isset($field['layouts']) || empty($field['layouts']))
            return $field;
        
        foreach($field['layouts'] as $layout_key => $layout){
            
            // Target layout name: hero
            if($layout['name'] === 'hero'){
                
                // Disable
                unset($field['layouts'][$layout_key]);
                
            }
            
        }
        
        // return
        return $field;
        
    }
    

    In this example, the layout named hero is being targeted. This will disable the layout globally (but it will be still present in the field configuration).

    Notice: if you already saved posts with the said layout (before implementing the code), the front-end have_rows() or the_flexible() functions will still display it. You will have to manually re-save each posts in order to fix this.

    To avoid that, a solution would be to use acf/load_field instead of acf/prepare_field. But acf/load_field is touchy, because it’s used everywhere in WordPress. This means the layout will disapear from the field configuration. You could use get_current_screen() to check if you’re in the ACF administration or in post edition.

    But in my opinion acf/prepare_field is the best solution here.

Viewing 1 replies (of 1 total)
  • The topic ‘Option to enable/disable flexible content layouts’ is closed to new replies.