I needed the same thing, showing a metabox based on the selected page template, and since the user has to select a page template and save and only then i could know which metabox to show i ended up showing all and use some simple jQuery to only show the needed one without having to save first, here:
function custom_metabox_per_template() {
global $pagenow,$typenow;
if ( is_admin() && in_array( $pagenow, array( 'post.php', 'post-new.php' ) ) && $typenow == 'page') {
$script = <<< EOF;
<script type="text/javascript">
jQuery(document).ready(function($) {
//hide all metaboxs
function hide_all_custom_metaboxes(){
$('#full-with.php').hide();
$('#showcase.php').hide();
$('#no-sidebar-page.php').hide();
}
//show a metabox
function show_custom_metabox(meta_id){
var selector = "#"+meta_id;
if( $(selector).length)
$(selector).show();
}
//first hide all metaboxes
hide_all_custom_metaboxes();
//then check for selected page template and show the corect metabox
var current_metabox = $('#page_template').val();
show_custom_metabox(current_metabox);
//and last listen for changes update when needed
$('#page_template').bind("change", function(){
hide_all_custom_metaboxes();
show_custom_metabox($('#page_template').val());
});
});
</script>
EOF;
echo $script;
}
}
add_action('admin_footer', 'custom_metabox_per_template');
the trick is to give your metabox id that matches the name of your template file, you can see in this example: full-with.php
, showcase.php
, no-sidebar-page.php
are the names of the theme files that define the page templates, and when i user changes the page template the shown metabox changes as well.