Good catch on the locate_template() as the safe fallback!<div><br></div><div>Chip<br><br><div class="gmail_quote">On Wed, Aug 24, 2011 at 2:07 PM, Otto <span dir="ltr"><<a href="mailto:otto@ottodestruct.com">otto@ottodestruct.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">If your code is in a plugin file, the right way to include other files<br>
is either relative to yourself:<br>
<br>
include 'whatever.php';<br>
<br>
Or to use the full path with plugin_dir_path if you need the full path<br>
for some reason:<br>
<br>
include plugin_dir_path(__FILE__).'whatever.php';<br>
<br>
<br>
In a theme, if you're including a file of functions that is in the<br>
theme directory, then the correct way is as Chip said:<br>
<br>
include_once( get_template_directory() . '/whatever.php' );<br>
<br>
However, if this is a template file or one that can be overridden by a<br>
child, then the correct way is actually to use locate_template:<br>
<br>
locate_template( 'whatever.php', true );<br>
<br>
Using locate_template is safe. Using the get_stylesheet_directory for<br>
an include is not safe, because the file may not be overridden by the<br>
child. The get_stylesheet_directory function will always return the<br>
path of the child theme if it is a child theme. The locate_template<br>
function checks the child theme for the file, and then checks the<br>
parent if the file isn't in the child. The "true" makes it go ahead<br>
and include the resulting file that it finds.<br>
<font color="#888888"><br>
-Otto<br>
</font><div><div></div><div class="h5"><br>
<br>
<br>
On Wed, Aug 24, 2011 at 1:51 PM, Chip Bennett <<a href="mailto:chip@chipbennett.net">chip@chipbennett.net</a>> wrote:<br>
> For including functional (as opposed to template) files in a Theme, you<br>
> should use something like the following in functions.php:<br>
><br>
> include_once( get_template_directory() . '/relative/path/to/file.php' );<br>
><br>
> Unless the file is intended to be over-ridden in a Child Theme, in which<br>
> case you would use:<br>
><br>
> include_once( get_stylesheet_directory() . '/relative/path/to/file.php' );<br>
><br>
> I'm sure Plugins would be similar, but that's a question outside the scope<br>
> of Theme-Reviewers. :)<br>
> Chip<br>
><br>
> On Wed, Aug 24, 2011 at 1:39 PM, Ryan Frankel <<a href="mailto:ryan.frankel@gmail.com">ryan.frankel@gmail.com</a>><br>
> wrote:<br>
>><br>
>> All,<br>
>><br>
>> I have a dumb question that I am sure has been covered a million times<br>
>> but I am not sure what the best way to include PHP files into my<br>
>> plugin/theme. What I currently have is something like below which can't<br>
>> possibly be the right way to do it.<br>
>><br>
>> include_once(WP_PLUGIN_DIR .<br>
>> '/swamped-event-calendar/php/functions_db.php');<br>
>> include_once(WP_PLUGIN_DIR .<br>
>> '/swamped-event-calendar/php/functions_calendar.php');<br>
>> include_once(WP_PLUGIN_DIR .<br>
>> '/swamped-event-calendar/php/functions_list.php');<br>
>><br>
>> Any tips on this would be greatly appreciated.<br>
>><br>
>> Ryan<br>
>> _______________________________________________<br>
>> theme-reviewers mailing list<br>
>> <a href="mailto:theme-reviewers@lists.wordpress.org">theme-reviewers@lists.wordpress.org</a><br>
>> <a href="http://lists.wordpress.org/mailman/listinfo/theme-reviewers" target="_blank">http://lists.wordpress.org/mailman/listinfo/theme-reviewers</a><br>
><br>
><br>
> _______________________________________________<br>
> theme-reviewers mailing list<br>
> <a href="mailto:theme-reviewers@lists.wordpress.org">theme-reviewers@lists.wordpress.org</a><br>
> <a href="http://lists.wordpress.org/mailman/listinfo/theme-reviewers" target="_blank">http://lists.wordpress.org/mailman/listinfo/theme-reviewers</a><br>
><br>
><br>
_______________________________________________<br>
theme-reviewers mailing list<br>
<a href="mailto:theme-reviewers@lists.wordpress.org">theme-reviewers@lists.wordpress.org</a><br>
<a href="http://lists.wordpress.org/mailman/listinfo/theme-reviewers" target="_blank">http://lists.wordpress.org/mailman/listinfo/theme-reviewers</a><br>
</div></div></blockquote></div><br></div>