1

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!

  • 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 Feb 10 '17 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 Feb 10 '17 at 14:47
0

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' );
  • 1
    Hi Tunji, I added the die() so I could see whether or not the function was being called. I can confirm that it doesn't work without the die() either – Mike Feb 10 '17 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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