Multiple WordPress themes and plugins often rely on the functionality of custom fields. We hear about custom fields really often, but rarely get explanation about this topic. So what are custom fields and how theme authors, plugin authors, and users can benefit from them?
What is WordPress custom fields
Custom fields are used to add extra information to your pages, posts or custom post types. This information is called metadata and can represent from simple titles to expiration dates. Theme and plugin authors use custom fields to add additional options to their product so you can go far beyond regular WordPress blog.
- Options to mark posts as promotional;
- Overwrite default theme settings for specific posts;
- Add unique identification;
- Add a custom background to posts.
These are just a few examples of how theme and plugin authors (like WooCommerce) can extend the capabilities of their products. I am sure you can think of much more as well.
Ok, so custom fields play an important role for themes, plugins, and WordPress in general. In fact, we can ‘blame’ custom fields for changing the way how we perceive and work with WordPress today.
How do custom fields work
The data (meta-data) of custom fields consists of key/value pairs. The key is the ‘name’ of the field and must stay the same for all posts within post type. The value, on the other hand, is the data that will change from post to post as your posts will use the value of the key to display it.
To get the idea behind key and values, let us imagine a simple example. We have a custom post type for fashion trends and want to display collection associated with a certain post. In this example, ‘Collection’ will be the key as it will be the same for all posts, while the name of the collection will be the value that will change from post to post (ex. ‘Street Smart’). As you can guess, we can do the same with fashion designer by using another custom field.
Now, to display data from custom fields on your posts you can use several ways depending on your goals.
The easiest one would be to use the_meta(); the function which will do nothing but display custom field by using CSS style post-meta. The key to the field will be displayed in a span with the post-meta-key style applied.
To me, this is by far the easiest and also most useless way to display meta-data. This will also display all of your custom fields for this post. So let us look into an appropriate way to do this.
get_post_meta($post_id, $key, $single);
Instead, you can use get_post_meta where you can specify the key of the custom field you want to display. As for the $single, it can be either true or false where true will return a single value as a string and false returns array. False came in very handy in case you have several values for the same key and want to have an array to operate with them.
With these simple output types, you have endless possibilities to operate with custom fields.
Advanced Custom Fields
We talked about custom fields and how to display them, nevertheless it all requires some coding. Completely ok if you are a developer and completely not if you are a regular user.
In addition, WordPress basic custom field options are limited in terms of types and this is a drawback for more advanced projects. And here custom one of the most popular WordPress plugins – Advanced Custom Fields. Advanced Custom Fields (ACF in short) allows you to add custom fields without writing a code. Moreover, you can specify a type of fields from a wide list, including:
- WYSIWYG editor
- select box
- date picker
- color picker
Well, this is something to work with on a real project, right? Add to this ability to create rules where to display custom fields on your WordPress admin and you can build a rocket.
Now, I will not cover the functionality of ACF here as this could be a totally different story and we will leave it for another day. Also, ACF by far is not the only plugin for custom fields so make sure to visit the WordPress plugin repository.
More about custom fields
There is always more about WordPress and there is more to learn about custom fields to uncover great opportunities. If you are into this, I suggest you visit the official WordPress Codex section related to custom fields and go from there.
If you are a regular WordPress user, then it seems you should have enough information on custom fields. You should just enjoy the power of WordPress through awesome themes and powerful plugins that have taken care about custom fields for you.
Got anything to add to this story, share your experience with custom fields or share some ideas? Feel free to start a discussion.