How to Add Custom Fields to a WordPress Theme

Did you know that you could add custom data to posts in WordPress? Technically, we’re talking about metadata, which is basically data about data. With WordPress, you can add metadata to a post using “custom fields”. Using custom fields basically lets you add more information about a post.

WordPress lets you display custom metadata alongside post content. For example, if you’re a blogger who writes about books, you can add a custom field for the book you’re currently reading. Likewise, these fields can be used to set a mood or even to display a rating for a product or service.

There is no limit to the kind of metadata you can add about your posts. However, most themes do not come with custom field support by default. Support for custom fields will usually require some coding. You can add the code manually to theme files or as a plugin.

However, in this article, we will provide an easy code snippet you can copy and paste into the functions.php file for any theme you are using. Then, all you need to do is add the shortcode “[meta]” to any post, and you will see a list of your post meta appear with the post content.

How to Add Custom Meta to the Theme’s Function File

  1. Log into WordPress Dashboard
  2. Use your favorite file management method to access the functions.php file in your theme directory
  3. Add this code to the functions.php file:
    function easyWPMetaShortcodeFun()
    {echo the_meta(); } 
    add_shortcode('meta', 'easyWPMetaShortcodeFun');
  4. Add this shortcode to any post that you wish to display custom fields for: [meta]

You should now know how to add custom meta fields to posts in your WordPress site. For more detailed information about adding and editing post meta for WordPress, you are best advised to check out the WordPress Codex regarding custom fields.

Was this article helpful? Let us know!