Plugin Development

Display Post Meta Box On Specific Page Templates

In WordPress you have the ability to create different post types to hold different content, along with the create different post types you can also add new content fields attached to the posts themselves. This will allow you to create a field to enter data linked to the post which can be very useful when you are creating a way of displaying products, you can enter the description of the product in the content field then you can enter the product attributes in their own content fields.

The benefit of this means that you can then use a custom template to display the product, this will allow you to display the new custom fields on the page.

To create new custom field meta box on your posts you need to use the WordPress function add_meta_box(). I have previously wrote a tutorial about how to create post meta data on to a post.

If you are using multiple page templates in your website, you may want some post meta to appear on some pages and not on others. In this tutorial we are going to find out how we can add post meta boxes only for certain page templates.

Create Post Meta Boxes

To create post meta box you first need to use the add_meta_boxes() function on the add_meta_boxes action to create the callback function that makes the post meta data.

add_action('add_meta_boxes', 'add_product_meta');
function add_product_meta()
{
    add_meta_box(
                 'product_meta', // $id
                 'Product Information', // $title
                 'display_product_information', // $callback
                 'page', // $page
                 'normal', // $context
                 'high'); // $priority
}

function display_product_information()
{
    // Add the HTML for the post meta
}

The above is an example of how you would create a post meta box for your new custom fields, but the problem is this will appear on every page and we only want this to appear when you have selected a certain page template.

To check the page template we need to access the post variable.

Whenever WordPress loads post data it sets a global variable of $post, we can use this to get the information of the current selected post. As we are going to search for the page template when editing the page the post variable will be populated. Once the post variable is populated we will have access to the page ID, we will then use this to look up the page template which is stored against the page as custom post meta data with a name of _wp_page_template.

Doing these checks inside the add_product_meta function.

add_action('add_meta_boxes', 'add_product_meta');
function add_product_meta()
{
    global $post;

    if(!empty($post))
    {
        $pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);

        if($pageTemplate == 'page-templates/product-page.php' )
        {
            add_meta_box(
                'product_meta', // $id
                'Product Information', // $title
                'display_product_information', // $callback
                'page', // $page
                'normal', // $context
                'high'); // $priority
        }
    }
}

function display_product_information()
{
    // Add the HTML for the post meta
}

The page template is stored on a page meta data information therefore we can use the function get_post_meta() to get the information, then make sure that this is the product page template file. If it is the correct page template then we can run the function to add the meta box.

This ensures that we are only creating the post meta boxes when we are using a certain page template.

Back to top

Learn how to code with Treehouse

  • Learn projects with access to 1000+ videos
  • Practice live with our Code Challenge Engine
  • Get help in our members-only forums

Start with a 7 day free trial

Comments

  1. Brian says:

    Hey so i am trying to add MultiPost Thumbnails on just certain pages in the admin area. So this plugin is setup to just add a bit of code to the functions file and then the meta boxes show up on all the pages in the admin area to add the secondary or thrid featured images... here is that code
    if (class_exists('MultiPostThumbnails')) {

    new MultiPostThumbnails(array(
    'label' => 'Secondary Image',
    'id' => 'secondary-image',
    'post_type' => 'page'
    ));

    new MultiPostThumbnails(array(
    'label' => 'Third Image',
    'id' => 'third-image',
    'post_type' => 'page'
    ));

    new MultiPostThumbnails(array(
    'label' => 'Fourth Image',
    'id' => 'fourth-image',
    'post_type' => 'page'
    ));
    }

    So that adds three more post thumbnail boxes on to all the pages when you edit them in the admin area. But I want to add them to certain pages so that i can change then name of the titles of the upload boxes so i can be more specific so i tried you code and tried targeting the front-page.php template but cant get it to work here is that code.
    add_action( 'init', 'multiple_images_meta' );
    function multiple_images_meta()
    {
    global $post;

    if (!empty($post)) {
    $pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
    if ($pageTemplate == 'front-page.php') {

    if (class_exists('MultiPostThumbnails')) {

    new MultiPostThumbnails(array(
    'label' => 'Secondary Image',
    'id' => 'secondary-image',
    'post_type' => 'page'
    ));

    new MultiPostThumbnails(array(
    'label' => 'Third Image',
    'id' => 'third-image',
    'post_type' => 'page'
    ));

    new MultiPostThumbnails(array(
    'label' => 'Fourth Image',
    'id' => 'fourth-image',
    'post_type' => 'page'
    ));
    }
    }
    }
    }
    any ideas on how i can get this to work that would be awesome thanks in advance

    1. Paul says:

      Hi, you should have a look at this tutorial as it will show you how to create custom post meta and save the data against a certain post https://paulund.co.uk/create-custom-meta-boxes