<?php
/*
* Change Meta Box visibility according to Page Template
*
* Observation: this example swaps the Featured Image meta box visibility
*
* Usage:
* - adjust $('#poststuff .your_page_box''#postimagediv') to your meta box
* disable- change 'page-portfolio.php' to your template's filename
* - remove the console.log outputs
*/
add_action('admin_head', 'wpse_50092_script_enqueuer');
function se50092_script_enqueuerwpse_50092_script_enqueuer() {
global $current_screen;
if('page' != $current_screen->id == 'page') {return;
$echo =echo <<<HTML
<script type="text/javascript">
jQuery(document).ready( function($) {
/**
* Adjust visibility of the meta box at startup
*/
if($('#page_template').val() == 'page-portfolio.php') {
// show the meta box
$('#postimagediv').show();
} else {
// hide your meta box
$('#poststuff .your_page_box''#postimagediv').showhide();
}
} else {
// Debug only
// hide- youroutputs metathe boxtemplate filename
// - checking for console existance to avoid //$('#poststuffjs .your_page_box').hide();errors in non-compliant browsers
if (typeof console == }"object")
console.log ('default value = ' + $('#page_template').val());
/**
* Live adjustment of the meta box visibility
*/
$('#page_template').live('change', function(){
if($(this).val() == 'page-portfolio.php') {
// show the meta box
//$('#poststuff .your_page_box''#postimagediv').show();
} else {
// hide your meta box
//$('#poststuff .your_page_box''#postimagediv').hide();
}
}
console.log ('live change value = '// +Debug $(this).val());only
}if (typeof console == "object");
console.log ('live change value = ' + $(this).val());
});
</script>
HTML;
echo}); $echo;
} </script>
}HTML;
add_action('admin_head', 'se50092_script_enqueuer');}