Skip to content

How to Add Custom Fields to Your WordPress Posts?

Custom Fields

Once you’ve gotten to know WordPress, you might want to start pushing its limits. This entails trying out some of the platform’s more complex features. Learning how to construct custom fields, for example, could be beneficial.

You can assign more data to your WordPress article by adding a custom field. As a result, you can add unique information or features to specific postings. You could, for example, use a custom field to notify WordPress which of your posts are sponsored, and then add code to your theme file that only displays a disclosure statement on those posts.

An Overview of Custom Fields

By default, the WordPress post editor is quite flexible, allowing you to alter your content to the nth degree. You are free to include almost anything in your posts. However, as your content grows, you may begin to wish for better ways to organise and manage it.

Custom fields are a more advanced WordPress feature that allows you to add more information to certain posts. This data is referred to as’metadata.’ Developers will appreciate custom fields and metadata since they can be used to augment posts with all kinds of coding. They can, however, be useful for more general WordPress users.

Let’s imagine you wanted to make it clear which of your blog entries are sponsored in order to be completely upfront with your readers. You may include a brief disclosure statement with each relevant post. Alternatively, you may save time by displaying a relevant message in a custom field. Then, in your theme file, you may include code to have your disclosure statement appear on the appropriate posts.

Don’t worry if this sounds complicated. Using custom fields is much easier than it appears. In reality, we’ll show you how to implement this example in the section following. If you’re interested in alternative uses for custom fields and metadata, the WordPress Codex entry on the subject is a good place to start.

Custom Fields in WordPress Posts: How to Include Them (In 2 Steps)

Custom fields may appear to be a little esoteric at first. Let’s have a look at an example to see how this feature works in practise. This general procedure can be used in a wide range of situations. You might include status updates in your posts, as well as a disclosure notice for sponsored content.

However, you should take a moment to back up your website first. You’ll be making modifications to your theme’s primary file if you follow these steps, and you don’t want to make any mistakes that will be permanent. It’s also a good idea to build a child theme and utilise it instead of your base theme for further protection.

1. Assign New Metadata to Your Post and Enab.le Custom Fields

The first step is to open a post to which you’d like to add a custom field. It could be an old or new post. Once you’re in the article, go to the upper right corner of your screen and click the Screen Options tab:

You may control what appears and doesn’t display in your posts here. Make sure the box entitled Custom Fields is checked (it isn’t generally checked by default). Simply shut the Screen Options menu after that.

You’ll see a new area below the post editor if you scroll down.

You’ll provide some metadata under Name and Value that describes the information you wish to include in this article. Some choices may already be included under Name, depending on the themes and plugins you’ve installed. In this case, however, you’ll want to establish new metadata.

Give your metadata a unique name. It can be about anything, but it’s better if it’s short and descriptive. We’ll call it Sponsored Post to continue our previous example of showing a disclosure statement on specific postings. Then we’ll just type “Yes” in the Value box to indicate that this post is sponsored.

After you click Add Custom Field, your metadata will be given to your post. Remember to save or change the post as well.

2. Include conditional code in your theme’s code

The preceding step informed WordPress about an important aspect of your post: whether it is sponsored or not. Now you must include instructions so that your website knows what to do. As previously stated, this does require some coding. Don’t let it deter you, though. Even if you aren’t a programmer, you should find the process rather simple.

To access the editor, go to Appearance > Editor in WordPress. You can look through and edit the files that make up your website here. You’ll want to go to the right-hand sidebar and look for the Single Post file (also known as single.php).

This is where you’ll paste the code that tells WordPress what to do when your custom fields are triggered. Depending on what you want to perform, the exact code you’ll use will differ. In our case, you’d want to include the following snippet:

<?php
$meta = get_post_meta( get_the_ID(), ‘Sponsored Post’ );
if( $meta[0] == ‘Yes’ ) {
?>
<p>This post is sponsored content, and we received a free copy of the product in order to conduct our review.</p>
<?php } ?>

Then, on the Update File button, click. This code tells WordPress to look for the Sponsored Post custom field in a post and see if the value is set to “Yes.” If this is the case, the notice will be displayed. Nothing will be added to the post if there is no custom field or the Sponsored Post option is set to “No.”

Also, the location of the code determines when it appears in the post. For example, you could put it before this line in the single.php file to make it appear at the top of the page:

while ( have_posts() ) : the_post(); Hopefully, you can see how custom fields might be beneficial. There are a lot of things you can do with this feature, so don’t be scared to experiment and see what you can come up with. Managing Your Custom Fields Using Plugins You now understand how to include custom fields and information in WordPress articles. What if, on the other hand, you want more freedom from this feature or simply want to make the procedure easier? Because we’re talking about WordPress, there are bound to be plugins that can assist you.

There aren’t many plugins that deal with custom fields, but there are a few good ones. Advanced Custom Fields is an excellent example.

The process of adding custom fields to WordPress is made easier with this popular, free plugin. It also expands the types of metadata that can be contributed, such as to users, media, and comments. Finally, it includes utilities that give you more ways to show custom field values in your theme files. There’s also a paid edition with even more features.

Custom Field Suite is a good alternative if that plugin appears overkill — as it might be for non-developers.

The process of adding custom fields to WordPress is made easier with this popular, free plugin. It also expands the types of metadata that can be contributed, such as to users, media, and comments. Finally, it includes utilities that give you more ways to show custom field values in your theme files. There’s also a paid edition with even more features.

Custom Field Suite is a good alternative if that plugin appears overkill — as it might be for non-developers.

Conclusion

Custom fields and metadata are two terms that can be a little perplexing at first. With time and patience, you’ll discover that they allow you to get even more from the WordPress platform. Fortunately, there are a few useful plugins that can assist you.