[wp-hackers] Include in functions.php

David Law wp-hackers at google-adsense-templates.co.uk
Wed Nov 16 00:31:33 UTC 2011


On Tue, 15 Nov 2011 13:48:39 -0500, you wrote:

>Is it an external PHP? If so, my suggestion is to create a new folder in your theme directory and then include this code in your functions.php file (change the file name and directory as needed) .
>
>$function_include = dirname( __FILE__ ) . '/external/example.php' ;
>if ( file_exists( $function_include) ) require_once( $function_include );
>
>that way, you can update the script later without affecting your functions.php file. If it'll always exist, you can skip the test.
>
>Chris

Simpler code for files added to the themes directory is

<?php include (get_template_directory() . "/extracode.php"); ?>

My themes functions.php file is only includes like the above (has no
actual code). Even include an additional file for user custom code
(filename zz_extra_functions.php) so it's easy for theme users to add
custom code to the functions.php file and not have to edit the
functions.php file everytime there's a theme update, just have to not
update that file.

Use the same idea for custom widgets as well for when a text widget
won't work.

Since my theme has so many features built in when a feature isn't used
the include file isn't loaded (based on options selected by the user)
reducing the amount of code loaded. For example have the option of
using 6 montization methods built in (AdSense, Chitika etc...) if the
user doesn't use those ad networks the code is never loaded.

Thousands of theme users, no issues so far with this approach.

David
-- 
http://www.stallion-theme.com/ Stallion WordPress SEO Theme
http://www.stallion-theme.com/stallion-wordpress-seo-plugin Stallion
WordPress SEO Plugin

>
>On 2011-11-15, at 1:41 PM, Pasquale Puzio wrote:
>
>> Hi guys,
>> 
>> I would import an external script in a custom function hooked to a
>> WordPress action.
>> 
>> Unfortunately, the include function seems to have some problems. So I'm not
>> able to use the functions defined in the script.
>> 
>> What's the best way to include an external script in WordPress?
>> 
>> Thanks
>> 
>> -- 
>> Dott. Pasquale Puzio
>> http://www.pasqualepuzio.it
>> info at pasqualepuzio.it
>> 340 5026359
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers


More information about the wp-hackers mailing list