I want to know custom page template name (the one chosen in page attributes - template) in PHP, before page opens in order to enqueue additional scripts according to chosen template. I know how to get this information in javascript admin frontend part, however it would be more convenient if I could get it in PHP.


You can use this code once set page template for specific post. If the template isn't set, you cant enqueue your script.

if ( $post->_wp_page_template === 'sky-template.php' ) {

Remember to replace sky-template.php with your template file name.


You can get some help from this question. _wp_page_template to dynamically use template

  • Thank you for effort. However, $post is not available outside the loop. I wrote I want to know the value of template in admin screen, while editing page. Suppose template is already set and we are now editing page which was created earlier. – Lovor Sep 4 '20 at 21:35

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.