4

I'm trying to make change to a site where with the current infrastructure isn't great as due to how it's been built over the previous site rather then fresh.

I have a section of the site which needs it's own custom fields. This section doesn't warrant it's own post type, and due to the state of the site it's extremely impractical to do it this way. What I don't want for the sake of the user is to have to make them use the custom fields as it's pretty sloppy.

So, how can I add fields (like you would for a custom post type) when a certain template is selected?

2
  • Even a starting point would be good, if anyone knows a hook or where you might find the current template selection in a variable on the post edit page.
    – Leonard
    Oct 30, 2012 at 13:11
  • It's been 3 years since this question was asked, but I wanted to add that now Advanced Custom Fields lets you use fields conditionally for specific templates. So no need to handle it yourself in code. Jan 28, 2016 at 19:30

1 Answer 1

13

Can you do that? Absolutely! You simply need to query the _wp_page_template meta key value of the $post object, and act accordingly. Perhaps something like so:

// Globalize $post
global $post;
// Get the page template post meta
$page_template = get_post_meta( $post->ID, '_wp_page_template', true );
// If the current page uses our specific
// template, then output our post meta
if ( 'template-foobar.php' == $page_template ) {
    // Put your specific custom post meta stuff here
}

Now, I would recommend using a custom post meta box, rather than custom fields.

While full implementation of custom post meta boxes is slightly outside the scope of your question, the underlying answer remains the same. I'll try to point you in the general direction, though. You'll use a combination of add_meta_box(), called in a callback hooked into add_meta_boxes-{hook}, a callback to define the metabox, and a callback to validate/sanitize and save custom post meta.

function wpse70958_add_meta_boxes( $post ) {

    // Get the page template post meta
    $page_template = get_post_meta( $post->ID, '_wp_page_template', true );
    // If the current page uses our specific
    // template, then output our custom metabox
    if ( 'template-foobar.php' == $page_template ) {
        add_meta_box(
            'wpse70958-custom-metabox', // Metabox HTML ID attribute
            'Special Post Meta', // Metabox title
            'wpse70598_page_template_metabox', // callback name
            'page', // post type
            'side', // context (advanced, normal, or side)
            'default', // priority (high, core, default or low)
        );
    }
}
// Make sure to use "_" instead of "-"
add_action( 'add_meta_boxes_page', 'wpse70958_add_meta_boxes' );


function wpse70598_page_template_metabox() {
    // Define the meta box form fields here
}


function wpse70958_save_custom_post_meta() {
    // Sanitize/validate post meta here, before calling update_post_meta()
}
add_action( 'publish_page', 'wpse70958_save_custom_post_meta' );
add_action( 'draft_page', 'wpse70958_save_custom_post_meta' );
add_action( 'future_page', 'wpse70958_save_custom_post_meta' );

Edit

It may be better to wrap the entire add_meta_box() call in the conditional.

4
  • 2
    Beautiful! That answer has surpassed any answer I expected completely. Thank you, you are a legend!
    – Leonard
    Oct 30, 2012 at 13:39
  • 2
    Worked a charm... I'd up vote or select as an answer but I've only just joined this wing of stack exchange so hopefully my thanks here will suffice ;)
    – Leonard
    Oct 30, 2012 at 15:55
  • Happy to help, and welcome to WPSE! Oct 31, 2012 at 1:18
  • Normal isn't a priority, and will cause the metabox to not display. Default is the priority to use there. Otherwise, spot on! Thanks Nov 7, 2014 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.