About Hooks and Filters

Seeing that you’ve referenced 3 Genesis Developers, i’d like to answer this question using 2 Genesis code snippets as well as 2 WordPress examples of action hooks and filters.

Hooks

Different template files in Genesis include hooks which simply enable you to execute PHP code in that position of the template.

Here’s a visual guide which shows all the hooks included in Genesis.

If you look in the genesis > lib > framework.php file you’ll find all the theme specific actions hooks which load when the loop loads.

do_action( 'genesis_after_loop' );

The above hook executes after the WordPress hook named loop_end. You can use any hook included in WordPress in Genesis as well as all the Genesis theme specific hooks.

Here’s a very basic example of how to use both a WordPress and Genesis action hook in your child theme to add content in a specific position in your theme.

add_action( 'genesis_after_loop', 'add_text_after_loop' );
function add_text_after_loop() {
echo 'Hello World - Genesis After Loop Hook';
}

And here’s a very similar WordPress hook you can use in Genesis or any other theme to display content after the loop.

add_action( 'loop_end', 'add_text_after_loop_end' );
function add_text_after_loop_end() {
echo 'Hello World - WordPress Loop End Hook';
}

To test these hooks, you could add both snippets to your child themes functions file to see exactly where the text ‘Hello World’ displays. Then you can change the hook to any other using the visual hook guide to determine the location you want to output the text.

enter image description here

You can see in the above that the Hello World text displays after the comment form for both hooks in the same position.

Filters

Different files in Genesis include filters which enable you to modify the default output of a parent themes function.

The code for a filter in Genesis looks like this:

$post_info = apply_filters( 'genesis_post_info', '[post_date] ' . __( 'by', 'genesis' ) . ' [post_author_posts_link] [post_comments] [post_edit]' );

The above line of code can be found in the genesis > lib > structure > post.php file.

This is the code which includes the filter named genesis_post_info which you can use in your child themes functions file to modify the post info for author, date, comments etc:

add_filter( 'genesis_post_info', 'sp_post_info_filter' );
function sp_post_info_filter($post_info) {
$post_info = '[post_date] by [post_author_posts_link] [post_edit]';
return $post_info;
}

In the above example, i have removed the shortcode for the comments link so only the date, author and edit links will be returned and displayed after the entry title.

This is a classic example of how you can use a filter to change the original function via your child theme without editing the default functions code in the parent theme.

This filter is only applicable in Genesis and not available in WordPress.

Here’s an example of a custom function which works with both Genesis and any other theme as its included in the core WordPress files so works in any theme.

add_filter( 'excerpt_length', 'sp_excerpt_length' );
function sp_excerpt_length( $length ) {
return 50; // pull first 50 words
}

The above code enables you to modify the length of the post excerpt in any theme.

Source http://my.studiopress.com/snippets/

Leave a Comment