1

Is there a way to show custom page settings on specific page templates ONLY?

For instance I have a settings field: "Custom Settings". I don't want this to show on the "Default Page Template". I only want it to display if we select the "Custom Page Settings Page Template".

Here's my meta box code:

function cd_meta_box_add()  
{  
add_meta_box( 'icon-class-meta-box', 'Icon Class', 'ic_meta_box_cb', 'page', 'side', 'default' );  
}  
add_action( 'add_meta_boxes', 'cd_meta_box_add' ); 

function ic_meta_box_cb( $post)  
{  
$values = get_post_meta( $post->ID );   
$selected = isset( $values['ic_meta_box_select'] ) ? esc_attr( $values['ic_meta_box_select'][0] ) : ”;  

wp_nonce_field( 'ic_meta_box_nonce', 'meta_box_nonce' ); 
?>  
<p>  
    <label for="ic_meta_box_select">Class</label>  
    <select name="ic_meta_box_select" id="ic_meta_box_select">
        <option value=" " <?php selected( $selected, ' ' ); ?>> </option> 
        <option value="red" <?php selected( $selected, 'red' ); ?>>red</option>  
        <option value="blue" <?php selected( $selected, 'blue' ); ?>>blue</option>
        <option value="green" <?php selected( $selected, 'green' ); ?>>green</option>
        <option value="pink" <?php selected( $selected, 'pink' ); ?>>pink</option>
        <option value="orange" <?php selected( $selected, 'orange' ); ?>>orange</option>
        <option value="black" <?php selected( $selected, 'black' ); ?>>black</option>
    </select>  
</p>
<?php    
}
1

I typically use CSS and jQuery for this type of thing by hooking admin_head. This should be done client-site as a user has an option to select your template or another one after DOM load. Basically, all you do is check if the value is set for #page_template and toggle show/hide if your template is selected or not.

add_action( 'admin_head-post.php', 'metabox_switcher' );
add_action( 'admin_head-post-new.php', 'metabox_switcher' );

function metabox_switcher( $post ){

    #Isolate to your specific post type
    if( $post->post_type === 'page' ){

        #Locate the ID of your metabox with Developer tools
        $metabox_selector_id = 'id-of-your-metabox';

        echo '
            <style type="text/css">
                /* Hide your metabox so there is no latency flash of your metabox before being hidden */
                #'.$metabox_selector_id.'{display:none;}
            </style>
            <script type="text/javascript">
                jQuery(document).ready(function($){

                    //You can find this in the value of the Page Template dropdown
                    var templateName = \'template-folder/file-name.php\';

                    //Page template in the publishing options
                    var currentTemplate = $(\'#page_template\');

                    //Identify your metabox
                    var metabox = $(\'#'.$metabox_selector_id.'\');

                    //On DOM ready, check if your page template is selected
                    if(currentTemplate.val() === templateName){
                        metabox.show();
                    }

                    //Bind a change event to make sure we show or hide the metabox based on user selection of a template
                    currentTemplate.change(function(e){
                        if(currentTemplate.val() === templateName){
                            metabox.show();
                        }
                        else{
                            //You should clear out all metabox values here;
                            metabox.hide();
                        }
                    });
                });
            </script>
        ';
    }
}
  • This works perfect for what I was doing. Thanks =D – Derek Feb 13 '14 at 6:16
  • $post return nothing in my case, so post type check fails. I removed $post parameter and added it as a global variable and it fixed my issue. – Shaikh Aezaz Jan 11 '17 at 13:31
0
add_action('add_meta_boxes', 'add_product_meta');
function add_product_meta() {
    global $post;

    if(!empty($post)) {
        $pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);

        if($pageTemplate == 'page-templates/product-page.php' ) {
            add_meta_box(
                'product_meta', // $id
                'Product Information', // $title
                'display_product_information', // $callback
                'page', // $page
                'normal', // $context
                'high'); // $priority
        }
    }
}

function display_product_information() {
    // Add the HTML for the post meta
}
  • This will not work when you are going to create new page – Shaikh Aezaz Jan 11 '17 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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