最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

filters - theme_page_templates not working

programmeradmin0浏览0评论

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
  • You're viewing an individual page of the 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:59
  • Ohhh man, I'm such a noob! I was looking at a post not a page. Have just checked again and its working fine! Thanks @Milo – Mike Commented Feb 10, 2017 at 14:47
Add a comment  | 

2 Answers 2

Reset to default 1

The 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' );
发布评论

评论列表(0)

  1. 暂无评论