Skip to main content
1 of 3
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');
brasofilo
  • 22.1k
  • 8
  • 69
  • 264