admin管理员组文章数量:1387443
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?
1 Answer
Reset to default 0If 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;
}
本文标签: Conditionally check if page is using template from plugin directory
版权声明:本文标题:Conditionally check if page is using template from plugin directory 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744569654a2613260.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论