Possible to limit custom meta boxes depending on what page template is used?

Ive searched around and havent been able to find anything regarding using a specific “template” as a target to trigger when to show custom meta boxes…Is this possible, and if so how would you script this out?

If post/page ID’s are the only real feasible option then would it be possible to target a parent page ID rather than individual page IDs?

Read More

EDIT #1

To help further explain the particulars of project Im going to briefly outline why Id like to instantiate custom meta boxes for only particular page templates…

The way my site is currently setup is have a bunch of custom static pages for artists on a record label. Originally this was perfectly fine since they did not need and/or request a more dynamic solution – however now they are currently needing to frequently update their own bio/pics/etc for each artist profile page making my current setup not very useful.

Currently, there is a Recording Artists page that acts as an archive, and then all artist pages are children of that page.

Additionally, I also need to allow personalized widgets to each artists sidebar which is a key part of deciding which direction to go.

The only other alternative Ive considered but Im not sure if it would work is to convert the recording artist archive into a CPT archive instead, and then make each artist page a custom single page…

Related posts

Leave a Reply

2 comments

  1. I needed the same thing, showing a metabox based on the selected page template, and since the user has to select a page template and save and only then i could know which metabox to show i ended up showing all and use some simple jQuery to only show the needed one without having to save first, here:

    function custom_metabox_per_template() {
        global $pagenow,$typenow;
        if ( is_admin() && in_array( $pagenow, array( 'post.php', 'post-new.php' ) ) && $typenow == 'page') {
            $script = <<< EOF;
    <script type='text/javascript'>
        jQuery(document).ready(function($) {
    
            //hide all metaboxs
            function hide_all_custom_metaboxes(){
                $('#full-with.php').hide();
                $('#showcase.php').hide();
                $('#no-sidebar-page.php').hide();
            }
    
            //show a metabox
            function show_custom_metabox(meta_id){
                var selector = "#"+meta_id;
                if( $(selector).length)
                    $(selector).show();
            }
    
            //first hide all metaboxes
            hide_all_custom_metaboxes();
    
            //then check for selected page template and show the corect metabox
            var current_metabox = $('#page_template').val();
            show_custom_metabox(current_metabox);
    
            //and last listen for changes update when needed
            $('#page_template').bind("change", function(){
                hide_all_custom_metaboxes();
                show_custom_metabox($('#page_template').val());
            });
        });
    </script>
    EOF;
            echo $script;
        }
    }
    add_action('admin_footer', 'custom_metabox_per_template');
    

    the trick is to give your metabox id that matches the name of your template file, you can see in this example: full-with.php , showcase.php , no-sidebar-page.php are the names of the theme files that define the page templates, and when i user changes the page template the shown metabox changes as well.

  2. I would solve it like this:

    1) Advanced Custom Fields to generate custom meta boxes (containing custom fields managed through ACF) that will display selectively according to a set of rules
    ACF creating a new custom field

    2) Widget Logic to show a Text Widget only in the pages you need

    3) enable shortcodes in the Text Widget, and build one to suit your needs (display styled info according to some criteria)

    4) if you need, you can make custom functions to use with Widget Logic, i.e. is_artist('name-of-artist) or is_record_label('name-of-label')

    [UPDATE]

    These questions (duplicates) can be handy too:

    Custom meta box shown when template is chosen

    Toggle admin metabox based upon chosen page template