Custom fields are used by many plugins and themes and there is a high likelihood that you’ve used them before, even if you aren’t quite sure exactly what they are. If you aren’t entirely familiar with what they are for and how you can use them to make your life easier and your posts richer, then this article should shed some light on the topic of custom fields in WordPress.
These fields are part of the core WordPress functionality so if you’ve not really noticed or interacted with them before; you can do so right from your own WordPress site. However, by default they are not visible so you will need to make a quick change to the ‘Screen Options’ on the ‘Add New’ or ‘Edit Post’ page.
Once the box is checked, there should be a new metabox under the post editor.
Depending on the plugins and themes you have installed on your site, the dropdown menu under the ‘Name’ column might already have some custom fields to choose from. However, if you are working on a new site, there might not be any custom fields in use.
Before we begin creating a new custom field, let’s take a look at what they are and how they can be used. By looking at the Custom Fields metabox, you can see that there are two parts to a field: the Name and Value. These are hopefully quite self-explanatory. The Name contains the name of the field, while the Value text area contains the content. The Value text field can hold any textual data including URLs.
Why Would You Want to Create a Custom Field?
An example of when you might want to use custom fields on your site could be for a fitness blog. A piece of information that their readers might find interesting could be what the last workout the author completed.
In this instance, the site could have a custom field with the name ‘Most Recent Workout’. To create a new custom field, simply click on the ‘Enter new’ text and then fill out the name field and click ‘Add Custom Field’.
Don’t worry about adding the colon at the end of the field name as WordPress will include that when displaying the field on a post or page.
Other example uses of this type could include:
- Food blog using ‘Last meal’
- Music blog using ‘Currently listening to’
- Shopping blog using ‘Last purchase’
- Review blog using ‘Download link/Link to product’
Any information you think might be of interest to your readers and enhance your posts could be a suitable use for a basic custom field.
Once a custom field has been created, from now on when anyone creates a new post on the site, the newly created field will be available from the dropdown menu in the Custom Fields metabox. This means that each time a post is crated there is an easy way to log your last workout, what you are listening too or current location, as part of the post metadata.
The way custom fields are displayed on the ‘Add New Post’ or ‘Edit Post’ screens can also be customised. This can include where they are located on the page and any supporting information explaining the purpose of the field. Your custom fields can also be set to be a required field which means they must be filled in before a post can be published. This is ideal when working on multi-author blogs and wanting to ensure all your authors follow the same content posting guidelines. More on creating advanced custom fields later.
What to Do with the Data?
Now we know how to collect and store the data, it’s time to take a look at what can be done with it. One popular way to make use of the data stored in a custom field is to publish it on the post page so it is viewable by your readers.
To do this, you need to edit the files for your theme. These can be accessed from within the WordPress admin dashboard by selecting the menu item: Appearance > Editor and then selecting the ‘content.php’ file from the list on the right.
To ensure any changes you make aren’t overwritten when you upgrade your theme, I recommend you create a child theme of your existing WordPress template.
It’s also a good idea at this point to make a backup of the ‘content.php’ file before you edit it. A quick way to do this is to copy the code in the code window and paste it into a new Notepad document. If something goes wrong with your ‘content.php’ file, simply paste the copied version back into the editor window overwriting any changes you’ve made. Or upload it via FTP to your themes folder on your server.
Now we need to add this piece of code to the file where we want the custom field and its contents to be displayed: <?php the_meta(); ?>
As we want the custom field contents to be part of our post and close to the main post content, look for the line containing <div class="entry-content"> and then paste <?php the_meta(); ?> on the a new line below it.
Due to this positioning, the code the custom field data will be displayed below the post title but above the post content. You can experiment with different locations to see where you would like your fields to be output. By default, WordPress lists the custom fields as bullet list although it is possible to style the output yourself by editing the theme files again.
As each post can have an unlimited amount of different custom fields, multiple entries will be displayed in a list.
There you have it, a basic introduction to custom fields in WordPress. If you want to progress with custom fields then the free Advanced Custom Fields plugin is a great tool to use for creating the fields. With this custom field plugin for WordPress you can create the fields using a visual interface which allows you to create fields with the following properties and attributes, plus many more:
- Fields containing an image and image galleries
- File uploads
- Checkbox, radio buttons and true false buttons
- Page links
- User information
- Date pickers
With the ACF plugin, custom fields can also be set as required, forcing authors to enter a value when publishing a post.
Perhaps the most useful feature of this plugin is its ability to let you insert custom fields into a post by using a shortcode. This saves you from editing the theme files in most cases.
Lots of plugins make use of custom fields so hopefully this introduction has given you an understanding of what they are, as well as some ideas of how you could use them on your site. While using them does require some digging into the code, like most things in WordPress there are some great plugins that allow you to get started with them without getting your hands dirty.
Have you seen any good examples of custom fields being used on a site? If so please share below in the comments.