0

How can I require specific PHP files for specific templates without any hook?

Example: I've one PHP file "autocomplete.php' and once a page template "photographers.php'

Now I want to require 'autocomplete.php' file in functions.php file only for photographers.php template.

How can I do this? I've tried using is_page_template () but didn't success.

Here is code

if(is_page_template('photographers.php')) {   
    require get_template_directory().'/inc/autocomplete.php';
}

Here is screenshot http://prntscr.com/mchx6a

I've searched before submitted this question in this platform. And i got this post load/require specific php files for specific pages/templates/post types But this post didn't solve my problem. Then I've submitted this question

photographers.php is a custom template. And autocomplete.php is some of custom functions and some of the script. That's all functions and script need to only for photographers.php (Custom Template) so I like to load that function/script only for that custom template

  • 2
    Why not require it all the time? And why not require it from photographers.php? – kero Jan 26 at 16:45
  • That should work. Try a relative link. Use require_once rather than require. Consider using dirname(__FILE__) instead of get_template_directory – admcfajn Jan 26 at 16:50
  • @kero photographers.php is a custom template. and autocomplete.php is some of custom functions and some of script. that's all functions and script need to only for photographers.php (Custom Template) , so why i will load that functions and script in all templates/pages/posts/etc? so I like to load that functions/script only for that custom template. – Md Abul Bashar Jan 26 at 16:58
  • More ore less PHP functions shouldn't make a difference (depending on wheter you call them or not ;). As for scripts, you should wp_register_script them in your default files and then you can wp_enqueue_script them in the template - no need to include special PHP files – kero Jan 26 at 17:16
  • @kero I think you didn't understand my question, I want to include some of functions and script, not only script, so If i want to add php function in my theme then did i need to wp_register_script? – Md Abul Bashar Jan 27 at 0:59
0

I had a similar problem, and posted the question here Using Includes in Templates in Document Head .

And I figured out the solution is to use get_template_part(). From my answer (which I checkmarked as correct; the other answer didn't work):

The key is to use the get_template_part in your Template at the beginning (after the template header). The file specified in that function must be in your Theme's root folder, although I think it could also be in the theme's template folder.

get_template_part('my_custom_functions');

This will do the equivalent of a 'include' in a non-WP PHP code. It will 'include' the file called my_custom_functions.php .

See my answer in the link above for more info.

  • Thanks for help. I got help via facebook group, here is answer functions.php executes very early. it can't find your template, unless you hook it with a function, which runs after template is loaded. you can use wp_enqueue_scripts. function something_awesome() { //your is_page_template codes } add_action ('wp_enqueue_scripts', 'something_awesome'); And It's working for me. Best Regards – Md Abul Bashar Jan 27 at 1:05

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.