I'll add a couple thoughts to the excellent, existing answers:
- I suggest naming your template file using the convention
template-foobar.php
. Doing so helps differentiate it from other template and template-part files.
Unless you intend to make use of the Post Editor for your template file, you don't need to output the default Loop in your custom template file. For example, if you want your form, and nothing but your form, you would want to start with the following codebase:
<?php
/**
* Template Name: Foobar Form
*/
get_header();
// Your custom code goes here
get_footer();
Assuming you're working with your own Theme (or a child Theme, as with your example case in which you modify Twenty Twelve), you would add your functional code to functions.php
, either directly in the file, or via an included file.
- If you want this code available outside of a specific Theme, then you probably want to implement your code via Plugin. And in that case, you probably won't want to use a custom page template at all, but would want to look at using a custom shortcode.
- That said, you could still use a Theme custom page template; you would just have to create a new one for each Theme you use, and I would strongly recommend that you wrap any Plugin function call in a
if ( function_exists() )
conditional.
- You can define a custom page template in your Plugin, and then use a callback on
template_redirect
, template_include
, or page_template
to determine when to use it (for example, based on a specific page slug, or, better yet, via custom post meta defined by the Plugin).
I'll step through this last point.
In your Plugin, you'll need to define custom post meta for static pages:
function wpse77991_add_meta_boxes( $post ) {
add_meta_box(
'wpse77991_display_form',
__( 'Display Form', 'textdomain' ),
'wpse77991_display_form_meta_box',
'page',
'side',
'default'
);
}
// Hook meta boxes into 'add_meta_boxes_page'
add_action( 'add_meta_boxes_page', 'wpse77991_add_meta_boxes' );
Then, define the callback:
function wpse77991_display_form_meta_box() {
global $post;
$custom = ( get_post_custom( $post->ID ) ? get_post_custom( $post->ID ) : false );
$display_form = ( isset( $custom['_wpse77991_display_form'][0] ) ? 'true' : 'false' );
<input type="checkbox" name="_wpse77991_display_form" <?php checked( 'true' == $display_form ); ?> value="true" />
<label><?php _e( 'Display Form?', 'textdomain' ); ?></label>
}
Then, define the post-save callback:
function wpse77991_save_display_form_meta_data() {
global $post;
$custom = ( get_post_custom( $post->ID ) ? get_post_custom( $post->ID ) : false );
$display_form = ( isset( $_POST['_wpse77991_display_form'][0] ) ? 'true' : 'false' );
update_post_meta( $post->ID, '_wpse77991_display_form', $display_form );
}
add_action( 'publish_page', 'wpse77991_save_display_form_meta_data' );
add_action( 'draft_page', 'wpse77991_save_display_form_meta_data' );
Now, the edit-post page will display a meta box for static pages, that will allow the user to check a checkbox to "Display Form".
You can then query that custom meta data in a callback to template_redirect
, template_include
, or better yet, page_template
:
function wpse77991_page_template_include( $template ) {
global $post;
$custom = ( get_post_custom( $post->ID ) ? get_post_custom( $post->ID ) : false );
$display_form = ( isset( $custom['_wpse77991_display_form'][0] ) ? 'true' : 'false'
if ( 'true' == $display_form ) {
return( plugin_dir_path( __FILE__ ) . 'template-foobar.php' );
}
}
add_filter( 'page_template', 'wpse77991_page_template_include', 1, 1 );
All of the above is untested code, used here as a proof-of-concept for the process.