I just purchased the Thesis wordpress theme for it’s ease of use and how darn customizable it is. One of the things that is really cool about it is how it allows you to write custom functions and hook their output into different sections of the theme. Fanceh!
Well, knowing how much of an obnoxious brat I am, I decided to write a snippet of code that will allow me to add custom functions simply by placing them in a functions-enabled folder.
First, Create the Directory
In your wp-content/themes/thesis/custom/ folder, create a “functions-enabled” folder.
Second, Edit Your custom_functions.php
Now, we need to tell thesis what to do with your folder. I tossed together a simple function that checks to see if the functions-enabled folder exists, loops through the files inside it to see if any of them end with .php, and includes them. Open your custom_functions.php file located within your wp-content/themes/thesis/custom directory and add the following lines:
function load_thesis_custom_functions()
{
//make it a little easier to access the path to the functions
$functionPath=THESIS_CUSTOM.'/functions-enabled/';
//Check to make sure the directory exists and it's readable.
if(is_dir($functionPath) AND $handle=opendir($functionPath)) {
while(false !== ($file=readdir($handle))) {
if(file_exists($functionPath.$file.'.php')) {
//Loads custom Functions in the path.
include($functionPath.$file.'.php');
} elseif(file_exists($functionPath.$file.'/'.$file.'.php')) {
//Loads custom Functions which ahve their own folder.
include($functionPath.$file.'/'.$file.'.php');
}
}
}
//Runs the function to load all the custom functions!
load_thesis_custom_functions();
Great! Now it will look through the functions-enabled directory and load them into wordpress.
Third, A Function To Test With
Now you’ll need a function. Here’s a rather simple one that includes jQuery inside a web pages <head> element. Create a file called jquery.php inside of the functions-enabled folder, and place the following contents in it:
<?php
/**
* Adds JQuery to the <head> of the page
*/
function add_jquery()
{
?>
<script type="text/javascript" src="jquery.js"></script>
<?php
}
add_action('wp_head','add_jquery');
Congratulations! Now you no longer have to put each and every function inside of the custom_functions.php file, which means organizing these functions should be a snap!