1

I have a number of custom templates which are assigned to different pages in my admin.

Is it possible to hide/lock the templates dropdown for certain pages? For instance, I have an Events Calendar template which will only ever be assigned to the Events Calendar page and so there is no need for the client to see this in the dropdown box however it should still be accessible for admins.

Thanks for help or guidance.

2 Answers 2

1

The "How?"

To get rid of a meta box, you got several hooks straight after they were added inside ~/wp-admin/edit-form-advanced.php. The one you need it 'dbx_post_advanced'. The you just need to remove the meta box with remove_meta_box().

<?php
defined( 'ABSPATH' ) or exit;
/** Plugin Name: (#90253) Remove Page Template Drop Down */
add_action( 'dbx_post_advanced', 'wpse90235_remove_page_template_metabox' );
function wpse90235_remove_page_template_metabox()
{
    remove_meta_box( 'pageparentdiv', null, 'side' );
}

That's the only chance doing it.

They "Why?"

The page_attributes_meta_box() callback that builds the meta box uses wp_dropdown_pages() and runs the arguments through a filter called 'page_attributes_dropdown_pages_args' which has two arguments: $dropdown_args and $post. When look at the arguments, you'll see that there's the echo argument set to 0/false, which shouldn't output anything.

$dropdown_args = array(
    'post_type'        => $post->post_type,
    'exclude_tree'     => $post->ID,
    'selected'         => $post->post_parent,
    'name'             => 'parent_id',
    'show_option_none' => __('(no parent)'),
    'sort_column'      => 'menu_order, post_title',
    'echo'             => 0,
);

...Sadly the echo is forced right after that:

<?php echo $pages; ?>
1

One thing to consider: if you name the template file page-{$page_slug}.php or page-{$ID}.php (matching the page in the admin interface/area/backend) then it will load that template without it being a 'page template' in the dropdown.

See the Template Hierarchy Diagram in the Codex.

4
  • You're talking about the template hierarchy?
    – kaiser
    Commented Mar 11, 2013 at 1:57
  • 1
    yes I am. depending on whether or not he has complete control over the content, that could be an option.
    – Norcross
    Commented Mar 11, 2013 at 5:39
  • Edited your answer to reflect that. +1
    – kaiser
    Commented Mar 11, 2013 at 13:11
  • 1
    much appreciated.
    – Norcross
    Commented Mar 11, 2013 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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