Display Post Meta Box On Specific Page Templates

in Plugin Development

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.

Subscribe To Newsletter

Get weekly updates to your email