Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Possible Duplicate:
Toggle admin metabox based upon chosen page template

Is this possible to change editor screen immediately after someone picks some template from the drop down? I need a metabox shown only when page template is page-portfolio.php. I know I can use this code:

$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;

$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);

And than add my boxes using if statements. The problem is it doesn't work immediately. Is this even possible without very complex coding to do "catch" moment when user changes templates list without saving?

share|improve this question

marked as duplicate by Stephen Harris, toscho Apr 25 '12 at 14:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Are you using custom post types? –  weston deboer Apr 24 '12 at 16:28
    
@westondeboer Nope, forgot about this information. Right now I'm working with normal pages. –  smogg Apr 24 '12 at 16:35
    
ups, after publishing the answer, I saw the possible duplicate warning... the link Stephen Harris provides does the script enqueuing the proper way –  brasofilo Apr 24 '12 at 19:38
    
@StephenHarris Yeah Stephen, thank You. That's duplicate and there is my solution in question you provided. Sorry. –  smogg Apr 24 '12 at 20:17

1 Answer 1

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:

/* 
 * 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;
}
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.