Custom fields

Advanced Custom Fields

Support page Documentation
Go to Advanced Custom Fields
ACF banner
19th May, 2018

Advanced Custom Fields
By Elliot Condon

Advanced Custom Fields(ACF from now on) is the de-facto Custom Fields plugin for WordPress. For those who don’t know what this means, these plugins can add CMS-like functionality to wordpress.

Say you’ve running a photography blog and want to attach the EXIF data along each posts. You can do this via ACF by attaching fields(camera settings, exposure, location, etc) to the default post type and they will be available programmatically in the frontend.


This plugin is developer oriented and may not make the best sense to non-tech people.

Functionality chevron-down

After installing ACF from the plugin directory, you should have a Custom Fields option available in the sidebar. Head over there and add a new field group.

acf creating a new field group
acf creating a new field group

Say, for this example we would like to add a location field to the default post. We’ll name the field group to someone appropriate such as ‘Post’.

Now add a new field. In this section you can choose the field type as well as other options.

Add a new field settings
Add a new field settings

The Field label is what it will be referred to in the admin panel. Field name is what you’ll refer this field by in the frontend. I recommend having field names in lowercase and with underscores instead of spaces.

Everything else is optional. Default field type is the text field. If you change the field type, there will be more options available to you specific to that field type.

Now we’ll attach this field group to the default post type. To do this, set the field  location filter to ‘Post type’ – ‘is equal to’ – ‘Post’. This should be the default anyways.

Now create a new post(or select an existing one) and there should be a metabox looking like this:

acf metabox example
acf metabox example

Fill in some text. This will now be available in the frontend.

Now, open the content-single.php template(or whatever template your theme uses for single post type) and paste <?= get_field('location'); ?> in.

This will show the text you’ve entered in the field inside the backend. This is the basis of how custom fields work and in general, how content management systems work in general.


There’s more to this. You can attach the metabox in the admin sidebar(the one on the right), on the page post type, in your custom post type, even on taxonomies(and custom taxonomies too).

There’s also an option to setup a global ‘options’ page where you can setup commonly use fields.

You can also change their look and positioning inside the field group options, disable the default wysiwyg editor and a bunch of other stuff. This truly opens up new ways of developing your WordPress website.


There are over 20 fields available. Some of the most common ones are:

  • text
  • checkbox
  • select
  • button group
  • file
  • gallery
  • image
  • wysiwyg editor
  • date time picker
  • google map
  • flexible content(very useful)
  • Repeater(very very useful)
  • Link
  • Post Object
  • Relationship

with more on the way.

Of all these, the repeater field is super useful as it allows you to nest field groups. Best of all – you can have unlimited nesting. This is a very powerful capability which is hard to find even in modern CMSs.

Also checkout awesomeacf where you can find 3rd party fields.

API chevron-down

At its core, the best part of ACF is its simple and elegant api. There are two methods, get_field($name, $id) and the_field($name, $id)

While both are the same, get_field() gets the field while the_field() echoes out the field.

The $id parameter is the $id of the post(or page, taxonomy, user_id, etc) you want to retrieve the field from.

Say you’ve got two fields with the same name(eg: name is location), one is attached to the default post type while other is attached to a page post type. To get the location field of the post type, you’ll set the $id to the post’s id. Put the id of the page post type to get the location of that page. Of course, if you do this inside the loop you don’t need to provide the id as it will get the id off the $post variable.


There’s a lot more you can do with it. Checkout their documentation page for more information.

Documentation chevron-down

The documentation is by far, the best I’ve seen for a WordPress plugin – and one of the best for any commercial project(except stripe and airtable)

Everything is neatly organized and the there are heaps of guides available to help you implement specific functionality. You can do some pretty crazy stuff with it, like using the acf fields as a frontend form.

Checkout the guides/tutorial section for more.

Gotchas chevron-down

Mainly I would advise not switching themes made with ACF. After switching themes there is a lot of data lying around inside the meta table and this could create conflicts if new fields are made.

Other than that:

  • The devs had changed some stuff in the past breaking backward compatibility.  ACF has been pretty stable since then but you may want to check for compatibility issues before updating.
  • By default, ACF stores field details in the database. You can use acf-json(I highly recommend using this) to store them as json files inside your theme folder. So if you move your theme in an empty wp installation, your fields will still be there.
  • ACF specific functions like get_field(), the_field() etc. will break your website if you remove the plugin. So you need to have it installed.

Support chevron-down

If you’re having trouble implementing fields, your best bet would be to consult the documentation. If you still hit a roadblock, make a post in the forums – they are pretty active and the developers frequent the forums too.

If you’re still stuck, open a support ticket.

Pricing chevron-down

ACF is a completely free plugin – however most of the useful fields come bundled in the ACF Pro version.

ACF Pro retails for $25(Single site license) or $100(unlimited site license) with lifetime updates for both.

Once you’ve installed the pro plugin, add in your license key in Sidebar > Custom Fields > Updates to enable updates.


Previously the premium fields were available separately. As of ACF version 5, they are only available through the ACF Pro version.

We highly recommend getting the pro version as it contains important fields like the repeater and the flexible content field as well as the fact that it comes with lifetime updates.

TLDR chevron-down

With heaps of field types available, massive community support behind it and continuous updates, ACF is a must if you want to build a custom WordPress website nowadays.

Unlike other custom field plugins/CMSs, it has an intutive drag-and-drop builder to create field groups. This may not sit well with devs who like to create field groups programmatically(ACF can do that too) but in my experience, it is much more faster to just create field groups with the given UI.

Just beware of the gotchas during migration and development.


Note that ACF is not a replacement against content builder plugins. They are client oriented and come with their own styles and content blocks.

Technical Report chevron-down

Lines of code 18,957
Cyclometric complexity(CC) 18.63
Code violations

(0 criticals, 38 errors) - 86 violations

Uses custom Db table? No
CC Chart

Seems like bulk of the code is for setting up the UI for the fields. Internally, ACF uses the post_meta table to store data. For every value, ACF stores the key and value seperately in the database. This can make clog up the database pretty quickly.

You should be able to use native WordPress functions like get_post_meta to retrieve data. Just be careful when retrieving repeater field values – you’ll need to unserialize them before you can use them.

Data generated from Phpmetrics. Although the data found through our research has been produced and processed from sources believed to be reliable, no warranty, expressed or implied, is made regarding accuracy, adequacy, legality or reliability of any information.

Features

Drag and drop builder Yes
Conditional logic Yes
API Yes - possibly the best for a Wordpress plugin
JSON import/export Yes
WPML compatible Yes
Trial version No

Verdict

This is THE custom fields plugin for WordPress. Definitely a must if you want to develop a custom WordPress theme.

  • Excellent API and documentation.

  • Very popular in the dev community and heaps of 3rd party plugins.

  • Some edge cases can pollute your database. See the gotchas section.

5/5

star-full star-full star-full star-full star-full

Helpful disclosure

Our works are supported by earning an affiliate commision
when readers choose to purchase a plugin based our reviews.

Leave a Reply

Your email address will not be published. Required fields are marked *