Hide Selected Page Templates From Non-Administrators
Removes chosen templates, such as a landing page or a blank canvas, from the template picker for editors and authors, so only administrators can apply layouts that need care.
Opens WPIDE on
Code
// Templates only administrators may choose.
$wpide_admin_only_templates = array( 'blank', 'template-landing.php', 'page-no-title' );
add_filter( 'theme_page_templates', function ( $templates ) use ( $wpide_admin_only_templates ) {
if ( current_user_can( 'manage_options' ) ) {
return $templates;
}
return array_diff_key( $templates, array_flip( $wpide_admin_only_templates ) );
} );
Notes
List template file names (classic themes, such as template-landing.php) or slugs (block themes, such as blank) in $wpide_admin_only_templates; pages already using them keep working.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.