[theme-reviewers] Stupid Question of the Day

Otto otto at ottodestruct.com
Wed Aug 24 19:07:24 UTC 2011


If your code is in a plugin file, the right way to include other files
is either relative to yourself:

include 'whatever.php';

Or to use the full path with plugin_dir_path if you need the full path
for some reason:

include plugin_dir_path(__FILE__).'whatever.php';


In a theme, if you're including a file of functions that is in the
theme directory, then the correct way is as Chip said:

include_once( get_template_directory() . '/whatever.php' );

However, if this is a template file or one that can be overridden by a
child, then the correct way is actually to use locate_template:

locate_template( 'whatever.php', true );

Using locate_template is safe. Using the get_stylesheet_directory for
an include is not safe, because the file may not be overridden by the
child. The get_stylesheet_directory function will always return the
path of the child theme if it is a child theme. The locate_template
function checks the child theme for the file, and then checks the
parent if the file isn't in the child. The "true" makes it go ahead
and include the resulting file that it finds.

-Otto



On Wed, Aug 24, 2011 at 1:51 PM, Chip Bennett <chip at chipbennett.net> wrote:
> For including functional (as opposed to template) files in a Theme, you
> should use something like the following in functions.php:
>
> include_once( get_template_directory() . '/relative/path/to/file.php' );
>
> Unless the file is intended to be over-ridden in a Child Theme, in which
> case you would use:
>
> include_once( get_stylesheet_directory() . '/relative/path/to/file.php' );
>
> I'm sure Plugins would be similar, but that's a question outside the scope
> of Theme-Reviewers. :)
> Chip
>
> On Wed, Aug 24, 2011 at 1:39 PM, Ryan Frankel <ryan.frankel at gmail.com>
> wrote:
>>
>> All,
>>
>>  I have a dumb question that I am sure has been covered a million times
>> but I am not sure what the best way to include PHP files into my
>> plugin/theme.   What I currently have is something like below which can't
>> possibly be the right way to do it.
>>
>>                        include_once(WP_PLUGIN_DIR .
>> '/swamped-event-calendar/php/functions_db.php');
>>                        include_once(WP_PLUGIN_DIR .
>> '/swamped-event-calendar/php/functions_calendar.php');
>>                        include_once(WP_PLUGIN_DIR .
>> '/swamped-event-calendar/php/functions_list.php');
>>
>> Any tips on this would be greatly appreciated.
>>
>> Ryan
>> _______________________________________________
>> theme-reviewers mailing list
>> theme-reviewers at lists.wordpress.org
>> http://lists.wordpress.org/mailman/listinfo/theme-reviewers
>
>
> _______________________________________________
> theme-reviewers mailing list
> theme-reviewers at lists.wordpress.org
> http://lists.wordpress.org/mailman/listinfo/theme-reviewers
>
>


More information about the theme-reviewers mailing list