最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Conditionally check if page is using template from plugin directory

programmeradmin6浏览0评论

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?

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?

Share Improve this question asked Apr 14, 2020 at 6:27 pixelngrainpixelngrain 1,3901 gold badge23 silver badges50 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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;
}
发布评论

评论列表(0)

  1. 暂无评论