Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Well, am having a very hard time understanding the difference between Actions and Filters. I do use actions in my code, but am a rookie and I don't know even a slightest bit of Filters.

I have been to codex, and various sites by NickTheGeek, BillErickson, Gary Jones etc, but no vein.

If you can tell me in simple words, with example that what basically is and difference between actions, filters and hooks.

Thanks a ton.

share|improve this question
    
Please make your question more specific. We need to know what you didn’t understand after reading the Codex. –  toscho Aug 18 '12 at 19:15
    
Or just read the answers we already gave you on the support forums: wordpress.org/support/topic/actions-filters-and-hooks –  Otto Aug 18 '12 at 19:48
    
Otto, sorry I posted it at both ends. But yes, am going through your piece. –  vajrasar Aug 18 '12 at 21:15
add comment

3 Answers

up vote 1 down vote accepted

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/

share|improve this answer
    
Thanks for the detailed response. –  vajrasar May 4 at 10:24
add comment

In my layman terms, Hooks as the name suggest is like placeholder where you can hook anything you want. To hook anything, you:

  1. Create function
  2. add_action('hookname','functionname');

For filters, the functions are already pre-built, but you want to edit and display a different one. So, first, you filter.

And FYI, to create a hook, you just:

  1. Create function with do_action e.g.

    function custom_hook() {
        do_action('custom_hook');
        echo 'Custom Hook works!';
    }
    
  2. List item

    <?php custom_hook(); ?>
    
share|improve this answer
3  
Not only that this is a bad advice (add global function that has hook in it - see here how to do it right - just add the hook directly in the template), it is also exactly the code from your question where you're telling that it's not working, right? –  kaiser Oct 14 '12 at 19:37
add comment

A filter filters content. An action executes an action.

For instance, post content is passed through a filter, e.g.:

echo apply_filters( 'the_content', $content );

That means that, if any filters are added, i.e. via add_filter( 'the_content', 'callback_name' ), the default $content is modified and returned by the specified callback.

An action, on the other hand, is simply a location where specified code is executed. For instance, wp_head is an action: a location in the template where code is executed (such as wp_enqueue_script(), etc.).

As usual, Otto has a great write-up that goes more in-depth.

share|improve this answer
    
Chip, thanks. Plus, am going through the link too. –  vajrasar Aug 18 '12 at 21:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.