I had previously talked about using WordPress’ built in “Hooks” to make WordPress do your bidding through Plugins or Themes. Did you know it’s possible to create your own “Hooks” within your Theme or Plugin? You can create a hook by inserting the following line of code where you want to execute your function:
do_action( 'my_hook' );
Now you just need to supply your function to run at this hook juncture. This is typically done in a functions.php file or within a plugin you are writing:
function my_function() {
/*Your code goes here*/
}
add_action( 'my_hook', 'my_function' );
When is this useful? Well, when developing a complex theme or plugin, I’ve found it easier to keep all your code organized by files and folders, taking what is a more modular approach. If you have a widget you are including in your custom theme, why not create a file specifically for this widget which allows it to be easily updated and included or excluded, and use a hook to pull in that content. It’ll make your main theme file much more human readable and allow for an easier understanding of the construction.
For more information on WordPress development check out the other posts in my ‘WordPress Dev 101′ series:

{ 0 comments… add one now }