WordPress Theme Development: Creating Custom Fields and Meta Boxes

WordPress Theme Development: Creating Custom Fields and Meta Boxes

Tania Rascia  /  145 responses

In the first part of my WordPress tutorial series, we learned what WordPress is and how to create and use a basic theme. In part two, we learned more advanced concepts like adding comments and images.

In part three, we’re going to learn how to add basic custom fields to a post, save them in the database, and show them on the front end of the website, without a plugin. And it’s going to be easy, you’ll love it.



  • Create a custom post type.
  • Add a custom meta box.
  • Create the following custom fields:
    • Input text field
    • Textarea
    • Checkbox
    • Select Menu
    • Image (and image upload)

This tutorial is independent of the previous installments. If you’ve been following along, you can just add this on to what you already have. If not, you can still do this tutorial from scratch as long as you have a basic knowledge of WordPress.

I’m still confused. What’s going on?

There are a lot of WordPress-specific words going around already, and it’s more confusing than I’d like. The gist of it is, have you ever wanted to add an extra field to a WordPress post? Maybe a date, or a URL, or an e-mail address? We’re going to learn how to do that. Have you ever wanted to add another upload field for images outside of the “Featured Image” thumbnail option? In this article, we’ll also learn how to add an upload button and browse through the media gallery to insert an image.

It’s basically a simplified version of what Advanced Custom Fields does, but without any plugins.

I promise I’ll make it as simple as possible.

Now we’ll begin.

Anything throughout this article prefixed with your_ is custom, and you can change the name.

Create a Custom Post

I’m starting off with a completely empty WordPress theme, just like in part one. I’m going to create a custom post called Your Post, with the id your_post. You can call it whatever you want, but it might be easiest to practice the first time around with the same names I used.

Here’s the code that will go into functions.php.


function create_post_your_post() {
	register_post_type( 'your_post',
			'labels'       => array(
				'name'       => __( 'Your Post' ),
			'public'       => true,
			'hierarchical' => true,
			'has_archive'  => true,
			'supports'     => array(
			'taxonomies'   => array(
	register_taxonomy_for_object_type( 'category', 'your_post' );
	register_taxonomy_for_object_type( 'post_tag', 'your_post' );
add_action( 'init', 'create_post_your_post' );

You can go more in-depth about all the options of creating a custom post here. I made a very simple one. The most important things that it does:

  • Registers a post type called Your Post, with the id your_post.
  • Supports a titlethe_title(), editorthe_content(), excerptthe_excerpt(), and thumbnail/featured image – the_post_thumbnail(). (Must add theme support for the thumbnail).
  • Supports taxonomies, or ways to group posts, with tags and categories.

Displaying the Custom Post

Now, to display the post on the front end. You can display the contents of a custom post anywhere. I’m just going to put it in page.php for testing purposes, so any page I make will display my custom loop.


$args = array(
	'post_type' => 'your_post',
$your_loop = new WP_Query( $args ); 

if ( $your_loop->have_posts() ) : while ( $your_loop->have_posts() ) : $your_loop->the_post(); 
$meta = get_post_meta( $post->ID, 'your_fields', true ); ?>

<!-- contents of Your Post -->

<?php endwhile; endif; wp_reset_postdata(); ?>

The variable $your_loop can be anything, I’m just sticking to a common theme of your_ throughout this article so you know what you can change. $meta = get_post_meta( $post->ID, 'your_fields', true ); is not necessary right now, but will be essential later.

Back End

Updating the title and editor fields like in a regular post…

Screen Shot 2016-08-09 at 3.16.33 PM


Inserting the template codes like normal…

<?php the_title(); ?>

<?php the_content(); ?>
Front End

And here we are. I have no styles or anything, because it’s not necessary for the point of the article, and I don’t like adding unnecessary complexity.

Screen Shot 2016-08-09 at 3.22.47 PM

Alright, so the custom post is all set up now.

Create a Meta Box

From here, the original source for much of the code comes from WordPress Meta Boxes: a Comprehensive Developer’s Guide by Alex Mansfield. I’ve attempted to simplify some of those processes here.

Here’s the code to add a meta box.

function add_your_fields_meta_box() {
		'your_fields_meta_box', // $id
		'Your Fields', // $title
		'show_your_fields_meta_box', // $callback
		'your_post', // $screen
		'normal', // $context
		'high' // $priority
add_action( 'add_meta_boxes', 'add_your_fields_meta_box' );

More detail on this function can be found here. The actual function here is add_meta_box( $id, $title, $callback, $screen, $context, $priority ). The most important ones are $title, which is Your Fields, and $screen (or page), which is where the meta box will be added. You can add it to a regular post or page, among other things, but I chose to add it to your_post, our custom post from earlier.

Now if you go back into your post, you’ll see this below the editor.

Screen Shot 2016-08-09 at 3.54.09 PM

It’s an empty meta box! Additionally, if you click on Screen Options at the top of the post, you’ll see Your Fields in the options.

Screen Shot 2016-08-09 at 3.56.09 PM

Neat. Now it’s time to start putting stuff in there.

Save Fields in the Database

First, the function that will display all your custom fields.

function show_your_fields_meta_box() {
	global $post;  
		$meta = get_post_meta( $post->ID, 'your_fields', true ); ?>

	<input type="hidden" name="your_meta_box_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>">

    <!-- All fields will go here -->

	<?php }

We’ll return to <!-- All fields will go here --> in a moment. For now, directly below the above function, we’re going to paste in this big chunk of code (modified from Paulund) that will save all your_fields to the database.

function save_your_fields_meta( $post_id ) {   
	// verify nonce
	if ( !wp_verify_nonce( $_POST['your_meta_box_nonce'], basename(__FILE__) ) ) {
		return $post_id; 
	// check autosave
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return $post_id;
	// check permissions
	if ( 'page' === $_POST['post_type'] ) {
		if ( !current_user_can( 'edit_page', $post_id ) ) {
			return $post_id;
		} elseif ( !current_user_can( 'edit_post', $post_id ) ) {
			return $post_id;
	$old = get_post_meta( $post_id, 'your_fields', true );
	$new = $_POST['your_fields'];

	if ( $new && $new !== $old ) {
		update_post_meta( $post_id, 'your_fields', $new );
	} elseif ( '' === $new && $old ) {
		delete_post_meta( $post_id, 'your_fields', $old );
add_action( 'save_post', 'save_your_fields_meta' );

Make sure your_meta_box_nonce matches the name attribute, and you’ve specified your_fields in the meta box function. This code is verifying the Nonce from the first function, making sure the user has the correct permissions to update the fields, and updating the post meta fields.

It’s a confusing block of code at first, but fortunately you don’t have to do much with this one besides make sure the id of the post matches the custom meta box you created.

Create Custom Fields

Now we’re going to return to <!-- All fields will go here -->. This is where we’re going to make our input text field, textbox, checkbox, select menu, and image upload. Right above that, we created a variable called $meta. This reaches into the your_fields table in the database and retrieves the information: $meta = get_post_meta( $post->ID, 'your_fields', true );. We’re going to make an array and put all of our custom fields in it.

You can use custom CSS to make your admin panel look nice, which I can possibly go into further if requested, but getting the basic functionality is more important to me for now, so I’m just going to use the default styles. Wrapping everything in a <p> isn’t semantically correct, but it’s keeping it organized for now.

If you see errors, look at the note at the bottom of the article.

Text Input

I’m going to add a regular text input. The regular-text class is just a built in WordPress admin style. Whatever you put in the straight brackets will be the code for your custom field. If I wanted to make this text field an e-mail address, for example on a custom post called “Team Members” with contact information for each team member, I might call it your_fields[email] instead of your_fields[text].

	<label for="your_fields[text]">Input Text</label>
	<input type="text" name="your_fields[text]" id="your_fields[text]" class="regular-text" value="<?php echo $meta['text']; ?>">

The code for the text area is almost the same as the input, except the value is echoed out between the tags instead of as an attribute. The rows, cols, and style placed on it doesn’t really matter. It’s important to leave no space between the tags, to ensure no extra space ends up in your textbox.

	<label for="your_fields[textarea]">Textarea</label>
	<textarea name="your_fields[textarea]" id="your_fields[textarea]" rows="5" cols="30" style="width:500px;"><?php echo $meta['textarea']; ?></textarea>

There might be several ways to implement the checkbox, but this is one way that worked for me.

	<label for="your_fields[checkbox]">Checkbox
		<input type="checkbox" name="your_fields[checkbox]" value="checkbox" <?php if ( $meta['checkbox'] === 'checkbox' ) echo 'checked'; ?>>
Select Menu

You can include as many options as you want here, but I’m just doing two for the example.

	<label for="your_fields[select]">Select Menu</label>
	<select name="your_fields[select]" id="your_fields[select]">
			<option value="option-one" <?php selected( $meta['select'], 'option-one' ); ?>>Option One</option>
			<option value="option-two" <?php selected( $meta['select'], 'option-two' ); ?>>Option Two</option>

The image upload is going to be the most complicated one. The actual display code isn’t any more complicated than the other fields have been. It’s important to include the meta-image class on the text input, and image-upload class on the submit button. I just put some quick max-width styling on the image for a simple preview.

	<label for="your_fields[image]">Image Upload</label><br>
	<input type="text" name="your_fields[image]" id="your_fields[image]" class="meta-image regular-text" value="<?php echo $meta['image']; ?>">
	<input type="button" class="button image-upload" value="Browse">
<div class="image-preview"><img src="<?php echo $meta['image']; ?>" style="max-width: 250px;"></div>

Actually getting this to do anything is the more complicated part. Directly below, you can place this JavaScript snippet (modified from Theme Foundation).

This code will open the built in WordPress media gallery when you click browse, and insert the selected URL into the input field.

    jQuery(document).ready(function ($) {
      // Instantiates the variable that holds the media library frame.
      var meta_image_frame;
      // Runs when the image button is clicked.
      $('.image-upload').click(function (e) {
        // Get preview pane
        var meta_image_preview = $(this).parent().parent().children('.image-preview');
        // Prevents the default action from occuring.
        var meta_image = $(this).parent().children('.meta-image');
        // If the frame already exists, re-open it.
        if (meta_image_frame) {
        // Sets up the media library frame
        meta_image_frame = wp.media.frames.meta_image_frame = wp.media({
          title: meta_image.title,
          button: {
            text: meta_image.button
        // Runs when an image is selected.
        meta_image_frame.on('select', function () {
          // Grabs the attachment selection and creates a JSON representation of the model.
          var media_attachment = meta_image_frame.state().get('selection').first().toJSON();
          // Sends the attachment URL to our custom image input field.
          meta_image_preview.children('img').attr('src', media_attachment.url);
        // Opens the media library frame.

And that’s everything that needs to go into the functions.php file! As with the CSS styling from earlier, we can place this JavaScript in a separate file and call it on the admin pages, but for simplicity I’m just including it in the functions.php file for now. At the end of the article, I’ll include the entire code in case you got lost anywhere along the way.

Display the Output

Now, we have all our fields, and they all appear in the Your Fields meta box. I’m going to fill them all out, check the checkbox, select an option, upload an image of the majestic Doge Lion, and publish the post.

These will all go in the $your_loop query. Make sure $meta = get_post_meta( $post->ID, 'your_fields', true ); is in your loop.

Screen Shot 2016-08-10 at 12.51.25 PM

Text Input

Simple output of the text field.

<h1>Text Input</h1>
<?php echo $meta['text']; ?>

Simple output of the textarea.

<?php echo $meta['textarea']; ?>

I’m going to check if the checkbox has been checked or not, and display a message based on it.

<?php if ( $meta['checkbox'] === 'checkbox') { ?>
Checkbox is checked.
<?php } else { ?> 
Checkbox is not checked. 
<?php } ?>

I’ll show you two things you can do with the select. The basic output will be the value attribute.

<h1>Select Menu</h1>
<p>The actual value selected.</p>
<?php echo $meta['select']; ?>

You can also use a PHP switch statement to display a specific message based on the value selected.

<p>Switch statement for options.</p>
	switch ( $meta['select'] ) {
		case 'option-one':
			echo 'Option One';
		case 'option-two':
			echo 'Option Two';
			echo 'No option selected';

And finally, displaying the image.

<img src="<?php echo $meta['image']; ?>">

Here is the final output of everything I filled out earlier.

Screen Shot 2016-08-10 at 1.14.30 PM


If you had any trouble following along the way, here are the final files for page.php and functions.php on GitHub Gist.

Functions Page

This article only goes over the most basic way to add custom fields and update the database. There are a thousand more ways to do it, and plugins like ACF that will do it for you as well. I think it’s useful to know how to interact with the database and learn how to do it without a plugin.

It’s also a good idea to sanitize the output of all your fields.

If you’ve found any errors, or have any additional information that would make this better or more simple to understand, please let me know!

Edit 2018:

To get rid of PHP warnings, change any instance of code that looks like this:

<?php echo $meta['textarea']; ?>

To this:

<?php  if (is_array($meta) && isset($meta['textarea'])){ echo $meta['textarea']; } ?>

Quality and morality on the web

Hi, my name is Tania Rascia. I've turned down every advertiser, sponsored post, and affiliate who has come to me. I give away all my knowledge for free to 300,000+ monthly readers and helped thousands of people learn and successfully transition into a design/development career.

You know what I stand for, and you know my commitment to you - no bullshit, no sponsored posts, no pop ups, no blocked content, ads, schemes, tactics, or gimmicks. Just free, quality content. If you enjoy my content, please consider supporting what I do!

Write a response

Your email address will not be published.

All code will be displayed literally.


  • sreejith cp says:

    What is the reason for showing warning message in my meta box input area . Warning message added below

    <br /><b>Warning</b>: Illegal string offset 'text' in <b>C:\xampp\htdocs\Biscotti\wp-content\themes\Biscotti\inc\biscotti-meta.php</b> on line <b>28</b><br /><br /><b>Notice</b>: Uninitialized string offset: 0 in <b>C:\xampp\htdocs\Biscotti\wp-content\themes\Biscotti\inc\biscotti-meta.php</b> on line <b>28</b><br />

  • Dedicated Manager says:

    What plugin or system are you using to display code? It looks great.

  • sreejith cp says:

    how to upload multiple image upload meta box in page

  • phil says:

    Great work Tania, would like to see more wordpress theme tutorials #4

  • Rahul Sharma says:

    How we can save the value of Radio Button ?

  • Rebeca Lopez says:

    Hi Tania! I'm very impressed with your tutorial, I've looking for a good tutorial, easier to understand and your tutorial is the easiest! Thank you so much, I would try it today.

  • Arqam Saleem says:

    Why you dont use input field with input type file to upload image ??? Is it possible with input type="file" !!

  • Laksha says:

    Hello Tania and about Custom functions, can do same? or different loading? thanks


  • Sunny verma says:

    Thank You

  • Cristina says:

    Hi, if I wanted to recreate this as a plugin, how would I go about this? Thanks

  • Anonymous says:

    thank you

  • johnson says:


  • Motahar Hossain says:

    Thank You.

  • Adrian says:

    In the startwordpress theme, Widgets(Dashboard >> Appearance >> Customize") disappear.
    How can I view Widgets or create "Search" separately?

  • homsavalia says:

    how to select multipal image select and display in mata box

  • Jekaterina says:

    This tutorial is great! Could you please advise how the same meta-boxes could be created for a page? Besides typing a 'page' instead 'your_post'?

  • Elia Kallas says:

    Thanks a lot Tania, I learned a lot from this tutorial, I love how you organize your work and help others, keep up the good work.

  • Philip says:

    Thanks tania great tutorials love your website and work

  • Kasper says:

    Inserting the template code like normal..
    <?php the_title(); ?>

    <?php the_content(); ?>"

    Where is it normal to put the code? I have no clue 🙂

  • wordpress theme developer says:

    Excellent article! This was so easy to follow and exactly what I was looking for. This was a great article with lots of good examples and resources. I absolutely like this article.


  • viksl says:


    if editor is not included in supports, the image upload doesn't work, it gives an error: Cannot read property 'frames' of undefined

    Might have something to do with wp_enqueue_media(); or something.

    Any advice? :).

    Thank you.

  • Andre Riis says:

    When i list this by tag ?? any fix for this ? or list by category ?? , seems it cannot find any since it only search in standard tags,category post..

    but everything else working perefect!!!!

  • kay says:

    Wow……I am blown away by your article. Great job!

  • Ryno Meyer says:

    Oh my word! Thanks a million for this. Very detailed and easy to follow. 5 stars!

    Thank you so much – I am a web developer that primarily works in developing my sites in HTML/PHP, as I love the power of what I can achieve with PHP – and always despised WordPress, due to it feeling limited.

    But this definitely opened a new and exciting door that will allow me to explore both!

  • Billu says:

    Using above code have some warnings or errors please see link: https://screenshot.net/v4x79tw

    Waiting for your reply. Thanks in advance

  • chris says:

    hi, great tutorial. but some reason my custom fields are not saving to the database? any ideas

  • Stephen says:

    Hi Tania, great article. I have a question : when you say “Make sure your_meta_box_nonce matches the name attribute, and you’ve specified your_fields in the meta box function”

    What “name attribute” are you talking about?

    • Dariusz says:

      I have been writing a plugin to show the distance for local attractions. So all the ‘your_fields’ I have named ‘distance_fields’ so my input name is ‘distance_fields[text]’ and my meta box nonce is named ‘distance_fields_meta_box_nonce’

      Hope this helps