How to Create WordPress Custom Fields to Add Metadata

Spread the love

WordPress comes with a custom fields feature for you to add any metadata to your posts. This can be information like a post-expiration date or simply the weather of that day. Here we show you the way to make good use of the custom fields to improve the functionality of your posts.

Content

Enable Custom Fields in the Block Editor

If you are still using the classic editor, the custom fields section is enabled by default. You should be able to find it below the text editor section.

For the block editor (Gutenberg), the custom fields section is disabled by default, but you can easily enable it. Just go to the three dots in the top-right corner of the screen and click it.

In the drop-down that appears, select Preferences at the bottom. Depending on the WordPress language that you set, it may say Options instead.

In the Panels section, toggle on the Custom fields option. It will then ask you to reload the page, so make sure that any unsaved work is saved first, or you will lose it.

A new Custom Fields section will now appear underneath the post.

Once you start adding custom fields, it will only show a maximum of 30 fields. You can keep adding more, but they won’t appear in the dropdown list.

Damien showed how to have more WordPress custom fields show by default. The tweak increases the menu list to a maximum of 120. You can either click through to the article, or here is a potted summary.

Go to Appearance -> Editor in your WordPress backend and look for the functions.php file. Add the following to the end.

function increase_postmeta_form_limit() {
	return 120;
}
add_filter('postmeta_form_limit', 'increase_postmeta_form_limit');

After saving the changes, refresh your WordPress backend page, and the number of custom fields in the menu will increase to 120.

Configure the Name and Value for the Custom Field

You can add anything you want as a custom field. Whether it’s the latest book you’re reading, or what the weather is that day, you can add whatever you want to your posts. For the sake of this post, let’s add how I’m feeling today. So click Enter new to begin.

In the Name section, add the title of what you want to add. This could be things like :

  • How am I feeling today?
  • The book I am reading today.
  • What am I listening to today?

You get the idea.

In the Value section, you now put the answer. In the case of my mood field, I said “I was not bad. A bit moody, but OK.” Remember, the custom fields are on a post-by-post basis, so what you enter on one post will not appear on another post.

It’s also worth clarifying that it doesn’t have to be a question in the Name section. It could also be something like standard text that you want at the beginning or end of your post. For example, maybe you want each post to have an affiliate disclaimer message? Or that it is a sponsored post? The possibilities are endless.

Note: For easier retrieval of the custom field via code, it is best to keep the Name short, all lowercase, and avoid any space. Link space with an underscore (_). For example, use “posts_to_redirect” instead of “Posts to Redirect”.

Once you have entered the required information, click Add Custom Field to save it. There doesn’t seem to be a minimum or maximum character count for a field, but for the sake of how it looks on your page, try not to go crazy. Brevity counts here.

Show the Custom Field in Your WordPress Theme

Unfortunately for us, life would be far too easy if that was all it took for a WordPress custom field to work. Now we have to make it show up on the page.

There is no easy way to display the custom field in your WordPress theme. You have to resort to inserting some PHP codes into your theme.

In the file where you want to display the custom field, use the following code:

$custom_field = get_post_meta($post_id, 'custom-field-name', true)
echo $custom_field;

A few things to take note of:

  • $post_id is the post ID of the post that you want to retrieve its custom field.
  • custom-field-name is the name you used for your specific custom field.

You can get more information here.

Tip: try using shortcode instead if you prefer to use an easier method to add metadata to your theme.

Add WordPress Custom Fields With a Plugin

If hacking your way through the WordPress installation files sounds like far too much work and heartburn, then you always have the option of a plugin. The most recommended plugin for WordPress custom fields is Advanced Custom Fields.

ACF uses the concept of field groups. These groups contain the custom fields you want to add to specific areas of your WordPress site, such as posts, pages, or custom post types. You define the field types, labels, and any specific settings within each group.

Developers can use ACF’s field types and functions to build powerful custom functionalities.

How to Install Advanced Custom Fields

Once you have installed the plugin, go to ACF’s settings page in the left-hand sidebar of your WordPress installation.

Go to Field Groups -> Add New. Here, you’ll name your field group. Click the Add Field button and choose your desired field type from the options. Each field type has its own settings to customize further.

Once you’ve built your field group, click Save Changes to save it. Now, when you edit a post or page assigned to that group, you’ll see the custom fields you created.

Now when you go to the specified post type, you’ll see the custom field below. Add the desired value into the text field.

ACF offers a free version with a good range of field types. Upgrading to ACF Pro unlocks even more features like repeater fields (for adding multiple instances of a field group) and flexible content layouts.

All screenshots by Mark O’Neill

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


Mark O’Neill

Mark O’Neill is a freelance tech journalist, editor, and bestselling spy fiction author. Originally from Scotland, he now lives in Germany with his wife and his dog.

Leave a comment