The genesis framework is a robust and dynamic WordPress theming framework created by Brian Gardner at StudioPress. Some key features that the Genesis Framework offers is lightning speed, excellent security (audited by Mark Jaquith), and durability.
Genesis Hooks one of the most useful features of this framework to extend your theme’s functionality. These hooks make development quicker and much more efficient.
A hook is a snippet of code that is written into a theme or plugin that allows the insertion of content into the theme. This allows you to extend the functionality of your website by hooking code into your website at certain points.
The Genesis hooks expound on the existing WordPress hooks for those who use the Genesis Framework.
When looking at Genesis Framework files, it is easy to spot hooks. Here is an example in the footer.php file:
[php]<!–?php genesis_before_footer(); ?–>
<div id="footer">
<div class="wrap"><!–?php genesis_footer(); ?–>
[/php]
The first line of this snippet is where the hook would be executed. Any content you hook in would be inserted after that. For a list of all of the Genesis hooks, please see the Genesis Hook Reference.
An Example of Hook Usage
A good example of how to use a hook to add a sidebar area at before the content of a page. This could be use for a slider.
Not all themes have a slider, so this example will show how to register a widget area after the header of a theme to insert a jQuery slider such as the Genesis Responsive Slider.
Step 1: The Hook
For the sake of simplicity, all of the code here will be written in your child theme’s functions.php file, but I typically use include files. Now, we want to tell the child theme to execute the function outlined in the step below directly after the header. With Genesis, it’s easy, we just insert a hook at the bottom of your functions.php file.
[code]
add_action( ‘genesis_after_header’, ‘include_sidebar_widget’ );[/code]
Step 2: Write the Function
Now we have to write the function. What this is doing is displaying the content of a widget (which will be a slider in our case) wrapped inside a div with the class of slider:
[php]
function include_sidebar_widget() {
echo ‘<div class="slider">’;
dynamic_sidebar( ‘slider’ ); // this is where the widget info gets inserterted
echo ‘</div>’;
}
[/php]
Register the Widget Area
Now we have to register the widetized area so we can insert the Genesis Responsive Slider (or any other slider widget) onto the page:
[php]
genesis_register_sidebar( array(
‘id’ => ‘slider’,
‘name’ => ‘Slider’,
‘description’ => ‘This is where a slider goes’,
) );
[/php]
Note: When declaring in an array, no upper-case letter or special characters can be used. The name key doesn’t have the same restriction.
The Results
If you go to your widgets page in the dashboard you will see an new widget area added called Slider. You now can drag your Genesis Responsive Slider widget there and you will see it pop up on every page of your site.
From here, you can get creative with your hooks using conditional logic to display a slider on the home page, a newsletter signup box on blog pages, and a contact form on sales pages. The possibilities are endless!
Here are all the lines I added to the functions.php file:
[php]
add_action( ‘genesis_after_header’, ‘include_sidebar_widget’ );
/** Loads a new sidebar after the content */
function include_sidebar_widget() {
echo ‘<div class="slider">’;
dynamic_sidebar( ‘slider’ );
echo ‘</div>’;
}
genesis_register_sidebar( array(
‘id’ => ‘slider’,
‘name’ => ‘Slider’,
‘description’ => ‘This is where a slider goes’,
) );
[/php]
Other Useful Resources
There are tons of useful resources on the web about the Genesis Framework. Here are some sites & resources I recommend for anyone looking dig deeper into the Genesis trenches:
Webistes
- http://briangarder.com – this site is jam packed with genesis resources and is the personal blog of the creator of the Genesis Framework
- http://designsbynickthegeek.com – another site jam packed with genesis tutorials
- http://my.studiopress.com/tutorials – tutorials by the site that makes genesis
Resources
- http://genesistutorials.com/visual-hook-guide/ – A great way to visualize where hooks are executed
Conclusion
Hooks in the Genesis Framework make WordPress even more extensible and make development much more efficient. It is hard to ignore the functionality of Genesis, and the strong community built around it.
Please stay tuned for more in depth uses of the Genesis hooks.
5 Comments