Skip to main content
Revised code: comments added, meta box div ID corrected
Source Link
brasofilo
  • 22.1k
  • 8
  • 69
  • 264
<?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');}
<?php
/*
 * Usage:
 * adjust $('#poststuff .your_page_box') to your meta box
 * disable the console.log outputs
*/
function se50092_script_enqueuer() {
    global $current_screen;
    if($current_screen->id == 'page') {
        $echo = <<<HTML
            <script type="text/javascript">
            jQuery(document).ready( function($) {
                if($('#page_template').val() == 'page-portfolio.php') {
                    // show the meta box
                    //$('#poststuff .your_page_box').show();
                } else {
                    // hide your meta box
                    //$('#poststuff .your_page_box').hide();
                }
                console.log ('default value = ' + $('#page_template').val());

                $('#page_template').live('change', function(){
                    if($(this).val() == 'page-portfolio.php') {
                        // show the meta box
                        //$('#poststuff .your_page_box').show();
                    } else {
                        // hide your meta box
                        //$('#poststuff .your_page_box').hide();
                    }
                    console.log ('live change value = ' + $(this).val());
                });                 
            });    
             </script>
HTML;
        echo $echo;     
     }
}
add_action('admin_head', 'se50092_script_enqueuer');
/* 
 * Change Meta Box visibility according to Page Template
 *
 * Observation: this example swaps the Featured Image meta box visibility
 *
 * Usage:
 * - adjust $('#postimagediv') to your meta box
 * - change 'page-portfolio.php' to your template's filename
 * - remove the console.log outputs
 */ 

add_action('admin_head', 'wpse_50092_script_enqueuer');

function wpse_50092_script_enqueuer() {
    global $current_screen;
    if('page' != $current_screen->id) return;
 
    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
                $('#postimagediv').hide();
            }
            
            // Debug only
            // - outputs the template filename
            // - checking for console existance to avoid js 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
                    $('#postimagediv').show();
                } else {
                    // hide your meta box
                    $('#postimagediv').hide();
                }
             
                // Debug only
                if (typeof console == "object") 
                    console.log ('live change value = ' + $(this).val());
            });                 
        });     
        </script>
HTML;
}
function usage
Source Link
brasofilo
  • 22.1k
  • 8
  • 69
  • 264
<?php
/*
 * Usage:
 * adjust $('#poststuff .your_page_box') to your meta box
 * disable the console.log outputs
*/
function se50092_script_enqueuer() {
    global $current_screen;
    if($current_screen->id == 'page') {
        $echo = <<<HTML
            <script type="text/javascript">
            jQuery(document).ready( function($) {
                if($('#page_template').val() == 'page-portfolio.php') {
                    // show the meta box
                    //$('#poststuff .your_page_box').show();
                } else {
                    // hide your meta box
                    //$('#poststuff .your_page_box').hide();
                }
                console.log ('default value = ' + $('#page_template').val());

                $('#page_template').live('change', function(){
                    if($(this).val() == 'page-portfolio.php') {
                        // show the meta box
                        //$('#poststuff .your_page_box').show();
                    } else {
                        // hide your meta box
                        //$('#poststuff .your_page_box').hide();
                    }
                    console.log ('live change value = ' + $(this).val());
                });                 
            });    
            </script>
HTML;
        echo $echo;     
    }
}
add_action('admin_head', 'se50092_script_enqueuer');
<?php
function se50092_script_enqueuer() {
    global $current_screen;
    if($current_screen->id == 'page') {
        $echo = <<<HTML
            <script type="text/javascript">
            jQuery(document).ready( function($) {
                if($('#page_template').val() == 'page-portfolio.php') {
                    // show the meta box
                    //$('#poststuff .your_page_box').show();
                } else {
                    // hide your meta box
                    //$('#poststuff .your_page_box').hide();
                }
                console.log ('default value = ' + $('#page_template').val());

                $('#page_template').live('change', function(){
                    if($(this).val() == 'page-portfolio.php') {
                        // show the meta box
                        //$('#poststuff .your_page_box').show();
                    } else {
                        // hide your meta box
                        //$('#poststuff .your_page_box').hide();
                    }
                    console.log ('live change value = ' + $(this).val());
                });                 
            });    
            </script>
HTML;
        echo $echo;     
    }
}
add_action('admin_head', 'se50092_script_enqueuer');
<?php
/*
 * Usage:
 * adjust $('#poststuff .your_page_box') to your meta box
 * disable the console.log outputs
*/
function se50092_script_enqueuer() {
    global $current_screen;
    if($current_screen->id == 'page') {
        $echo = <<<HTML
            <script type="text/javascript">
            jQuery(document).ready( function($) {
                if($('#page_template').val() == 'page-portfolio.php') {
                    // show the meta box
                    //$('#poststuff .your_page_box').show();
                } else {
                    // hide your meta box
                    //$('#poststuff .your_page_box').hide();
                }
                console.log ('default value = ' + $('#page_template').val());

                $('#page_template').live('change', function(){
                    if($(this).val() == 'page-portfolio.php') {
                        // show the meta box
                        //$('#poststuff .your_page_box').show();
                    } else {
                        // hide your meta box
                        //$('#poststuff .your_page_box').hide();
                    }
                    console.log ('live change value = ' + $(this).val());
                });                 
            });    
            </script>
HTML;
        echo $echo;     
    }
}
add_action('admin_head', 'se50092_script_enqueuer');
Source Link
brasofilo
  • 22.1k
  • 8
  • 69
  • 264

I know that the plugin Advanced Custom Field does that. Checking its code, I saw that he deals with this issue using jQuery. Using it as reference, I think this should work for you:

<?php
function se50092_script_enqueuer() {
    global $current_screen;
    if($current_screen->id == 'page') {
        $echo = <<<HTML
            <script type="text/javascript">
            jQuery(document).ready( function($) {
                if($('#page_template').val() == 'page-portfolio.php') {
                    // show the meta box
                    //$('#poststuff .your_page_box').show();
                } else {
                    // hide your meta box
                    //$('#poststuff .your_page_box').hide();
                }
                console.log ('default value = ' + $('#page_template').val());

                $('#page_template').live('change', function(){
                    if($(this).val() == 'page-portfolio.php') {
                        // show the meta box
                        //$('#poststuff .your_page_box').show();
                    } else {
                        // hide your meta box
                        //$('#poststuff .your_page_box').hide();
                    }
                    console.log ('live change value = ' + $(this).val());
                });                 
            });    
            </script>
HTML;
        echo $echo;     
    }
}
add_action('admin_head', 'se50092_script_enqueuer');