# How to require specific PHP files for specific templates

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

• 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

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 .