When working with WordPress plugins, it’s often expected that each one will have its own obligatory “Settings” page. It’s the place where we go to tweak various options to fit our particular needs.
Those of you who are familiar with Advanced Custom Fields have probably noticed that our plugin doesn’t have a classic settings page. But what you may not realize is that ACF does indeed have its own settings – you just manage them in a more developer-friendly sort of way.
Say what? 😲 Yes, it may be a bit of a surprise. But we’re here to let you in on the secret. We’ll take you on a tour of some incredibly useful settings and show you how to change them.
First thing’s first, let’s talk about where and how to change ACF settings. As we mentioned, there is no settings panel inside of WordPress. Instead, making changes requires a little bit of code in your active theme’s functions.php file. There are two coding methods you can use to access and change settings:
If you’re just looking to change a single setting, a filter will do the trick. Each ACF setting can be modified via its own filter: “acf/settings/{$name}”.
For example, if we wanted to change ACF’s path using a filter, we could do so by adding the following code to functions.php:
function my_acf_settings_path( $path ) {
return get_stylesheet_directory() . '/acf/';
}
add_filter('acf/settings/path', 'my_acf_settings_path');
Functions are generally a better choice for times when you want to change multiple ACF settings. The code is cleaner and more efficient – making for a tidier functions.php.
You’ll want to tap into the acf_update_setting()
function to make changes. It’s best to do so while wrapped in the acf/init action, like so:
function my_acf_init() {
acf_update_setting('show_admin', false); // Don’t show ACF in the WP admin.
acf_update_setting('google_api_key', 'xxx'); // Our Google API key.
}
add_action('acf/init', 'my_acf_init');
Notice how changing each setting requires just a single line of code. Using this method will make future maintenance much easier, as all of your settings changes will be located in one place.
Now that we’ve discussed the different ways to change a setting, let’s have a look at some of the more common settings you might want to tweak. Each one lets you customize ACF to match the needs of your workflow.
Using show_admin lets you choose whether or not to display the ACF menu item in the WordPress dashboard. This can be quite useful in that it prevents absolutely anyone from making unwanted changes to your custom field setup from within WordPress.
acf_update_setting('show_admin', false);
Alternatively, you might choose to show or hide the ACF menu item based on WordPress Roles and Capabilities. This prevents unauthorized users from making changes, while still preserving access for others. It’s also useful for situations where you have created your own custom roles and capabilities.
acf_update_setting('capability', 'delete_pages');
The l10n setting allows ACF to translate field and field group settings using the _() function. You’ll see this in action when exporting your custom field groups as PHP on the ACF Tools screen. Turning this setting off will allow you to override translation without the need to modify the textdomain.
acf_update_setting('l10n', false);
If you’re utilizing ACF’s Google Maps Field, you can set your API key to avoid running up against usage limits.
acf_update_setting('google_api_key', 'your-key-here');
By default, ACF enqueues multiple scripts when the plugin is activated. Some are required so that specific types of custom fields like datepickers and select boxes work as expected. But there are situations where you may already be loading one or more of these scripts via your theme or another plugin.
For example, the datepicker is a jQuery script that is registered (but not enqueued) by default in WordPress. Since it’s commonly used, separately loading it in via ACF may not be necessary.
ACF’s enqueue_xxx setting lets you turn off these scripts to avoid potential conflicts. There are a total of three scripts that can be turned off:
To prevent a script from loading, use the function like so:
acf_update_setting('enqueue_datepicker', false);
acf_update_setting('enqueue_datetimepicker', false);
acf_update_setting('enqueue_select2', false);
Select2 is a library that enables the use of custom select boxes. It’s quite popular and is used by many WordPress plugins and themes. However, conflicts can arise when attempting to run multiple versions of this library on a site. For example, if ACF is running version 4 and another plugin runs version 3, that could lead to some unexpected issues.
select2_version lets you choose which version of the Select2 library you’d like ACF to use. You can choose from version 4 (the default) or version 3.
acf_update_setting('select2_version', 4);
Those of you looking to boost the performance of the WordPress edit screen will definitely be interested in this setting. We set this to true by default which will remove the WP Custom Fields meta box (the basic custom field inputs that ship with WordPress core). This relatively small feature actually makes a huge difference, as it removes a lengthy SQL query when loading the edit screen. If you require the basic custom field inputs for what ever reason, set this back to false and your inputs will appear.
acf_update_setting('remove_wp_meta_box', true);
ACF is all about helping you build WordPress websites to match your exact needs. What’s great about the settings above is that they all offer an extra bit of flexibility to help you reach your goals. Whether it’s boosting performance, preventing conflicts or simply enabling you to do things your own way, there is more power at your fingertips than you might have imagined.
And, now that you know more about these settings – it’s time to put them to good use! Start experimenting with them and see how they can improve your development process.