I want to check if a page has a parent with a certain page template attached to it.

If I know that I can determine which scripts to load or not.
Normally I would just get the page template and if it is a match load the scripts needed but now I have the script in my functions.php file and can't get the $page->ID to check.

I don't really know how to solve this. In my functions.php:



add_filter( 'admin_post_thumbnail_html', 'function_name');
function functions_name( $myhtml ) {
    //do stuff

This hooks into an existing function. I can't check within the function because that would disable the complete function if the statement would be false.

  • i would rather edit the page template in question to include the scripts...
    – user9
    Feb 3 '16 at 16:24
  • But this is in the admin meta-box of the page so the template wouldn't be able to affect this right? Feb 4 '16 at 12:23
  • not sure what you exactly mean. you certainly can write php code and enqueue scripts in page templates
    – user9
    Feb 4 '16 at 13:22
  • Yes I know but the script I wrote is used in the admin page. It has effect on the featured-image panel. If I understand you correctly it should be possible to add this code to the template and let the code be executed if the template is assigned to a page? Feb 4 '16 at 13:49

This question has been answered on Stack Overflow before: https://stackoverflow.com/a/14626254/844732

add_action( 'admin_head', 'check_page_template' );
function check_page_template() {
    global $post;
    if ( 'page-homepage.php' == get_post_meta( $post->ID, '_wp_page_template', true ) ) {
        // The current page has the foobar template assigned
        // do something


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.