[wp-hackers] Include in functions.php

Otto otto at ottodestruct.com
Wed Nov 16 00:44:35 UTC 2011


On Tue, Nov 15, 2011 at 6:31 PM, David Law
<wp-hackers at google-adsense-templates.co.uk> wrote:
>>$function_include = dirname( __FILE__ ) . '/external/example.php' ;
>>if ( file_exists( $function_include) ) require_once( $function_include );
>>
>
> Simpler code for files added to the themes directory is
>
> <?php include (get_template_directory() . "/extracode.php"); ?>
>

Better way:

<?php locate_template('extracode.php', true); ?>

This does a "require" on the file to load it up and it also accounts
for child theme overrides. So if you want to override said file in a
child theme, then it's just a matter of copying the file to the child
theme and modifying it.

You can load files in subdirectories of the theme like so:

<?php locate_template('subdir/extracode.php', true); ?>

Set the optional third parameter to true to make it require_once the
file instead of just require.

-Otto


More information about the wp-hackers mailing list