With out-of-the-box WordPress you only get a limited set of input fields, but if your project is data driven you may need more in order to be able to display different types of data. I have used and created plugins to try and solve this problem, however the one near-complete solution I found is the Advanced Custom Fields plugin (ACF for short).
What are Custom Fields?
When creating pages and posts in WordPress you get the standard fields, such as the title, content, categories and featured image. Hidden under the screen options (in the top right-hand corner) there are additional fields you can check to activate, with one of them being custom fields. When you activate this option, a new panel will appear inside the editor underneath the content field. This will allow you to create a new custom field, with the options name and value. Just like the title field, you can enter a new field name (this could be something like title-2 if you want a second title) and you can then enter the value. After the page is saved you can retrieve the data and either display it as part of the content or display it somewhere else on the page.
This default WordPress solution allows for a lot of flexibility, as you can use custom fields to display whatever content you can, in any way you see fit. However it isn’t so straightforward and easy to use, not to mention the lack of support for different types of the data.
Advanced Custom Fields to the rescue
This time-saving plugin uses the same concept as the standard custom fields and takes it a bit further. ACF gives you new types of custom fields that you can assign to your pages, posts, custom template and custom post types. There is also the added benefit of being able to individualize the custom fields to specific scenarios by creating a set of rules to determine which edit screens will use these fields (e.g. custom page template, posts, pages, parent pages, and users types). This allows you to create and design specific fields for specific purposes.
ACF come packed with very useful PHP functions to use in your templates. The following below is retrieving a Flexible content data. Imagine you want a list of cars with different attributes:
while ( have_rows('cars') ) : the_row(); the_sub_field('name'); the_sub_field('model'); the_sub_field('make'); endwhile;
get_field($field_name) retries the value of the specified field. If you are not using a Repeater or Flexible content, the value returned is not a multidimensional array, so you wouldn’t need a while loop.
the_sub_field($field_name) works only with Repeater or Flexible content, and is used to to display a sub field value, while looping through the field. The non repeater counterpart to that is just
the_field($field_name), this works just the same as
The good, the bad and the add-ons
I didn’t run into many problems using the ACF plugin. There was only one slight issue that I couldn’t find a solution for – If you have a collapsed list of items in a Repeater or Flexible content, it’s difficult to tell which item is which as the label isn’t the name of the item. I solved this by using a helper plugin that uses jQuery and replaces the label with the name field of my items. This was a quick and easy solution that worked surprisingly quite well.
ACF 5 was released over the Easter weekend. It introduces many new features, visual settings, fields have been created and pretty much the main core has been rebuilt from the ground up for stable development and faster performance.
ACF 5 also says goodbye to add-ons and now has a simple free and pro version. Read more
The icing on this well baked cake for me was the beautiful layout and easy-to-read documentation, packed with code examples and a helpful Q&A section. The doc was pretty much my one stop shop for understanding how the plugin worked. All the add-ons in the pro version (mention above), are also well documented and very well supported. I was able to find support for many problems I encountered from the community forum.
ACF has a wealth of field types with some listed below:
- Wysiwyg Editor: Optional WP media insertion, basic/full editor buttons.
- File: Upload file or select from WP library, returns object, URL or ID.
- Gallery: Provides a simple and intuitive interface for managing a collection of images.
- Post Object: Returns WP object of any post or page, can be limited to specific post types or taxonomies
- Relationship: Same as post object, but with improved interface and drag-drop reordering
- Taxonomy: Returns object or ID for one or more taxonomy terms, can be limited to tags, categories or custom taxonomies
- Repeater: Allows you to create a set of sub fields which can be repeated again and again whilst editing content!
- Flexible content: Acts as a blank canvas to which you can add an unlimited number of layouts with full control over the order.
Overall this plugin is easy and userfriendly to use, very well documented, with a friendly support commnunity. With over 3 million downloads and 4.9 out of 5 stars with 686 votes, you can see that this is a very popular amongst developers. One reason for this may be because it adds full CMS capabilities to WordPress. ACF 5 release comes even more jam-packed with improvements and more features. If you are still not convinced on why you should use it, the best thing to do is take it for a spin, for free. Let ACF save you, head over to advancedcustomfields.com and get started today.