Support

Account

Forum Replies Created

  • I recently found that James’ solution to this problem (which I had been using successfully for some time) stopped working in my project. I did a little digging and discovered that two changes to the last line in his example are necessary to get it working again.

    First, the acf/get_field_groups filter was deprecated in ACF 5.7.11 in favour of acf/load_field_groups, so you’ll need to update accordingly. Simple enough, but it only gets you half way: If you dump the field group data before and after, you’ll see the titles changing properly, but for whatever reason those new titles don’t get used in the edit screen metaboxes.

    Second, you need to change the priority of the filter. WordPress filters default to a priority of 10, with numbers closer to 0 indicating earlier execution order. To ensure our changes don’t get overwritten, we actually want to apply this filter later, meaning we want a higher number. 20 seems to be working okay for me right now. I believe it became necessary to set priority in this case in ACF 5.7.10 with the addition of the _acf_apply_get_local_field_groups function, which also uses this filter at priority 20.

    TL;DR, James’ solution should work if we update the last line to read:
    add_filter('acf/load_field_groups', 'my_acf_change_metabox_title', 20);

  • Sorry to dredge this up again; I wanted to share some code I wrote that may benefit others who find this question. I was looking for a solution a bit lighter than the suggested plugin and a bit more user-configurable than John’s (excellent) solution above, and came up with the following.

    I used a lot of PHP7+ syntax here—you may need to do a little translating if you’re running a lower version on your server!

    
    new acf_custom_json_save_path_setting();
    
    class acf_custom_json_save_path_setting {
        // This will store the preferred save path for the group for later retrieval.
        private $preferred_save_path;
    
        public function __construct() {
            // Add the "JSON Save Path" setting to all field groups.
            add_action('acf/render_field_group_settings', [$this, 'add_json_save_path_setting']);
    
            // Call an early bird (priority 1) action before saving the field group.
            add_action('acf/update_field_group', [$this, 'set_up_save_path'], 1, 1);
        }
    
        public function add_json_save_path_setting($field_group) {
            // Create our custom setting field with the specified options.
            acf_render_field_wrap([
                'label'        => 'JSON Save Path',
                'instructions' => 'Determines where the field group\'s JSON file will be saved, relative to the active theme\'s directory.',
                'type'         => 'text',
                'name'         => 'json_save_path',
                'prefix'       => 'acf_field_group',
                'prepend'      => '/',
                'placeholder'  => 'Use default path',
                'value'        => $field_group['json_save_path'] ?? '',
            ]);
        }
    
        public function set_up_save_path($group) {
            // Get the preferred save path, if set.
            $preferred_save_path = $group['json_save_path'] ?? null;
    
            // If not set (or set to an empty string), do nothing.
            if (!$preferred_save_path) {
                return $group;
            }
    
            // Set aside the preferred path and add an override action.
            $this->preferred_save_path = get_stylesheet_directory() . "/$preferred_save_path";
            add_action('acf/settings/save_json', [$this, 'save_to_preferred_save_path'], 9999);
    
            // Return the group for updating as usual.
            return $group;
        }
    
        public function save_to_preferred_save_path($path) {
            // Ensure this field group is saved to the preferred save path.
            $path = $this->preferred_save_path;
    
            return $path;
        }
    }
    

    The code above will add a simple “JSON Save Path” text field to each field group’s Settings section, in which you can enter a path where you’d like to save the group’s JSON file. The path is relative to the active theme’s directory (I felt like that was a safe default), but you can use standard directory traversal methods like ../ to go up a few levels if you’re, say, trying to save something to a plugin directory.

    And as a reminder, this code doesn’t do anything to load from your custom save points. I looked into it, but ensuring the array of load points always has the correct paths in it gets complicated quickly. Thankfully, it’s very simple (and much more transparent) to manage your load points manually via the acf/settings/load_json filter.

  • Sorry to dredge this up again; I wanted to share some code I wrote that may benefit others who find this question. I was looking for a solution a bit lighter than the suggested plugin and a bit more user-configurable than John’s (excellent) solution above, and came up with the following.

    I used a lot of PHP7+ syntax here—you may need to do a little translating if you’re running a lower version on your server!

    
    new acf_custom_json_save_path_setting();
    
    class acf_custom_json_save_path_setting {
        // This will store the preferred save path for the group for later retrieval.
        private $preferred_save_path;
    
        public function __construct() {
            // Add the "JSON Save Path" setting to all field groups.
            add_action('acf/render_field_group_settings', [$this, 'add_json_save_path_setting']);
    
            // Call an early bird (priority 1) action before saving the field group.
            add_action('acf/update_field_group', [$this, 'set_up_save_path'], 1, 1);
        }
    
        public function add_json_save_path_setting($field_group) {
            // Create our custom setting field with the specified options.
            acf_render_field_wrap([
                'label'        => 'JSON Save Path',
                'instructions' => 'Determines where the field group\'s JSON file will be saved, relative to the active theme\'s directory.',
                'type'         => 'text',
                'name'         => 'json_save_path',
                'prefix'       => 'acf_field_group',
                'prepend'      => '/',
                'placeholder'  => 'Use default path',
                'value'        => $field_group['json_save_path'] ?? '',
            ]);
        }
    
        public function set_up_save_path($group) {
            // Get the preferred save path, if set.
            $preferred_save_path = $group['json_save_path'] ?? null;
    
            // If not set (or set to an empty string), do nothing.
            if (!$preferred_save_path) {
                return $group;
            }
    
            // Set aside the preferred path and add an override action.
            $this->preferred_save_path = get_stylesheet_directory() . "/$preferred_save_path";
            add_action('acf/settings/save_json', [$this, 'save_to_preferred_save_path'], 9999);
    
            // Return the group for updating as usual.
            return $group;
        }
    
        public function save_to_preferred_save_path($path) {
            // Ensure this field group is saved to the preferred save path.
            $path = $this->preferred_save_path;
    
            return $path;
        }
    }
    

    The code above will add a simple “JSON Save Path” text field to each field group’s Settings section, in which you can enter a path where you’d like to save the group’s JSON file. The path is relative to the active theme’s directory (I felt like that was a safe default), but you can use standard directory traversal methods like ../ to go up a few levels if you’re, say, trying to save something to a plugin directory.

    And as a reminder, this code doesn’t do anything to load from your custom save points. I looked into it, but ensuring the array of load points always has the correct paths in it gets complicated quickly. Thankfully, it’s very simple (and much more transparent) to manage your load points manually via the acf/settings/load_json filter.

  • I recently had issues making this work on some fields because their keys were formatted a little differently, causing the substr call to get the wrong value. You may be able to solve for this by working from the end of the field ID string rather than the beginning.

    substr($field['key'],-13)

    Of course, this is similarly (if not more) brittle, as it still depends on the format of ACF’s field IDs being consistent. Consider printing out your field IDs to take a look at them if you’re having trouble, and adjust accordingly.

    I definitely vote for this to be a first-class feature of the plugin—it’s much less confusing than just leaving the label field blank!

  • I personally didn’t want Flexible Content fields to collapse automatically, but I did want a way to do so quickly and easily. For those with similar needs, here’s how I’m handling that.

    Specifically, this adds a single “Collapse All” link to the top of Flexible Content fields.

    
    /**
     * Add quick-collapse feature to ACF Flexible Content fields
     */
    add_action('acf/input/admin_head', function() { ?>
        <script type="text/javascript">
            (function($) {
                $(document).ready(function() {
                    var collapseButtonClass = 'collapse-all';
    
                    // Add a clickable link to the label line of flexible content fields
                    $('.acf-field-flexible-content > .acf-label')
                        .append('<a class="' + collapseButtonClass + '" style="position: absolute; top: 0; right: 0; cursor: pointer;">Collapse All</a>');
    
                    // Simulate a click on each flexible content item's "collapse" button when clicking the new link
                    $('.' + collapseButtonClass).on('click', function() {
                        $('.acf-flexible-content .layout:not(.-collapsed) .acf-fc-layout-controls .-collapse').click();
                    });
                });
            })(jQuery);
        </script><?php
    });

    Note that this sort of thing is always subject to breaking with new ACF versions—generally you just need to keep an eye on class names.

  • Using unlabeled endpoint tabs to allow for fields between tab groups should definitely get added to the Tab field documentation! I’ve been wishing it was a thing for months before finally stumbling in here and realizing it was there all along.

    Just never though to leave a required field blank, you know? The hazards of being a hardcore rule-follower, I suppose.

Viewing 6 posts - 1 through 6 (of 6 total)