Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
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 '12 at 13:11
add comment

1 Answer 1

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)
            'normal', // priority
        );
    }
}
// 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.

share|improve this answer
1  
Beautiful! That answer has surpassed any answer I expected completely. Thank you, you are a legend! –  Leonard Oct 30 '12 at 13:39
    
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 '12 at 15:55
    
Happy to help, and welcome to WPSE! –  Chip Bennett Oct 31 '12 at 1:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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