I have, on an existing project, created a folder named templates in the theme root directory where I have put all my custom template files, all the files that have the Template Name: BlahBlahBlah in the top of the file.

This change broke places in the code where I had done this to get the file name: get_post_meta( $post_id, '_wp_page_template', true );.

I fixed it like this: basename(get_post_meta( $post_id, '_wp_page_template', true ));.

My question is if I broke something in WordPress by doing this change. Does anyone know?

1 Answer 1


Since WP 3.4 page templates can be stored in theme root folder or direct sub-directoryr, that is no problem. But rename page templates, which include moving files to other directories, will unassign that page templates for all pages currently using it.

To reassign the page template, simply edit the page, and reselect the desired template.

About the get_post_meta( $post_id, '_wp_page_template', true );, if the template file is under "mytheme/sub-dir/templae-file.php", the _wp_page_template value should be "sub-dir/template-fie.php". Using basename leave only "template-fie.php"; you may have reasons to need only the name of the file but it is not the correct value for _wp_page_template, so it is not broken.

  • Thanks! I use the filename to check for a specific file. I could always check on the whole path. The reason I do this is because I have a plug-in I only want on certain pages. Doing it this way works, but seems kinda shady.
    – ptf
    Jan 12, 2015 at 6:09

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.