I'm trying to filter the contents of my page template dropdown in the admin area. Having done some Googling it seems that the hook I need to use is theme_page_templates ... but this just does not run for me. I have no idea why but the code is not called at all. This is the code I'm using, but nothing happens.
function filter_template_dropdown( $page_templates ) {
die( var_dump( $page_templates ) );
// Removes item from template array.
unset( $page_templates['template-faq.php'] );
// Returns the updated array.
return $page_templates;
}
add_filter( 'theme_page_templates', 'filter_template_dropdown' );
I'm running the latest version of Wordpress (4.7.2) - any help would be much appreciated!
I'm trying to filter the contents of my page template dropdown in the admin area. Having done some Googling it seems that the hook I need to use is theme_page_templates ... but this just does not run for me. I have no idea why but the code is not called at all. This is the code I'm using, but nothing happens.
function filter_template_dropdown( $page_templates ) {
die( var_dump( $page_templates ) );
// Removes item from template array.
unset( $page_templates['template-faq.php'] );
// Returns the updated array.
return $page_templates;
}
add_filter( 'theme_page_templates', 'filter_template_dropdown' );
I'm running the latest version of Wordpress (4.7.2) - any help would be much appreciated!
Share Improve this question asked Feb 10, 2017 at 12:16 MikeMike 1156 bronze badges 2 |2 Answers
Reset to default 1The theme_page_templates
filter is not available anymore, please use the theme_templates
filter instead.
add_filter( 'theme_templates', 'filter_template_dropdown' );
You have a call to die()
in your function, which terminates the execution of your function and outputs the $page_templates as is.
To successfully remove the template-faq.php
from the available page templates, you should remove your call to die()
:
function filter_template_dropdown( $page_templates ) {
// Removes item from template array.
unset( $page_templates['template-faq.php'] );
// Returns the updated array.
return $page_templates;
}
add_filter( 'theme_page_templates', 'filter_template_dropdown' );
page
post type in admin when you try this? Your code works as expected for me- output is halted when the Page Attributes meta box is rendered. – Milo Commented Feb 10, 2017 at 13:59post
not apage
. Have just checked again and its working fine! Thanks @Milo – Mike Commented Feb 10, 2017 at 14:47