ACF Tips and Tricks (Advanced Custom Fields)

ACF (Advanced Custom Fields) is an amazing plugin for extending the already potent power of WordPress. Below I’ve collected some of the ACF tips and tricks I use, almost on a daily basis, to get the most out of it that I can.

Create a Theme Options Page

If you’re creating a theme for a client, you’ll at some point need somewhere to store global settings, hence the reason it’s a core part of ACF from version 5 upwards .

I’ve seen it before where developers have created a page (an actual page post type) called “settings”, then pulled all the fields from that page using it’s ID. Doing that makes a real mess and can lead to issues where the page ID may not match from one site to another. If both are using the same theme then you’ve got a headache on your hands.

Note: The only caveat with using theme settings is that they don’t get pulled across when using the WP Import plugin. Therefore the only reliable way around the issue I’ve found is to copy the entire database from local MySQL to the server version (via an SQL export file). Not always ideal, but it does mean your live version is identical to the local one.

Only Show “Custom Fields” in Menu to Certain Users

Let’s face it, clients don’t usually understand the inner workings of WordPress, or sometimes even the basics, therefore you need to stop them breaking things. They can create posts and pages, yet that tends to be the limit.

The last thing you want a client doing is clicking the “Custom Fields” menu link and messing with something. Especially since anything they change is almost guaranteed to break their site.

Using the below code you can hide the page and menu link form everyone except the specific usernames you specify. It doesn’t just remove the menu link either. If a another user, no matter their role, tries to access that “Custom Fields” page, they’ll be told they don’t have access.

Save and Load Fields from Your Theme

A big issue with ACF for theme development is having two versions of all your fields. One set lives locally and another lives on the live version. This is even more problematic when you have multiple sites using the same theme and fields.

The solution to that problem is ACF’s Sync feature. The way it works is by saving and loading the custom fields via JSON files in your theme.

Once you upload your theme you’ll get a new menu link show up on the “Custom Fields” page, titled “Sync”. Therefore every time you change some fields locally and upload your theme, you can simply click the “Sync” link on the live site to import the fields from your theme to the database.

If you’re wondering why you’d need to sync them at all, you actually don’t have to. However, if the theme changes on the site at any point it’s always good to have the fields saved in the database and not only be part of the theme.

Get Advertisements from Theme Settings

A handy trick I came up with when developing the latest version of Inspirational Pixels was to make the advertisements easier to manage.

By using an ACF “Group” field, I added all my advertisement codes to the theme settings page (mentioned above). I then created a function to check those ads are populated and pull them in if they are, just by using the field name.