I am having trouble loading the script conditionally in my plugin.

I have a DataTables page and want to load datatables scripts only for that page. So I made a page template called datatables-template.php and loading dynamically so can set the template in page attribute.

The template path is my-plugin/public/templates/datatables-template.php

So far so all is working fine. However, when I try to load script explicit fore that template using is_page_template()

When I have checked with get_page_template() that returning theme current page template which is page.php in my case.

Note: is_page working fine, of course, since it will check the current page, that would work. I have also checked my path using file_exists and that returns true. That means I am giving a correct path for the template. So no idea how to check it.

Additionally: I have tried with global $template which returns the correct plugin template but that doesn't work in conditional check in my plugin file.

global $template;

if('datatables-template.php' == $template){
    //load script

Question: How can I check if the page has set the specific page template in page attributes, in my case datatables-template.php and load script explicit for that page?

1 Answer 1


If I understand it correctly, you would like to check your template right before loading.

I think the hook template_include is the best place you could do it. Because this is the last place to load the template in loading procedure.

Regardless plugin or themes functions.php, it will also work.

The following is proved to be working in theme functions.php

add_filter( 'template_include', 'q364016_check_template' );
function q364016_check_template( $template ) {
    // your code

    return $template;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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