WordPress.org

Support

Support » Plugins and Hacks » Action Hooks versus Filter hooks – I’m confused

Action Hooks versus Filter hooks – I’m confused

  • Hi there,
    I’m currently starting WP plugin development. I’m a reasonable programmer although I’ve never worked with PHP before. Only .NET related technology.

    But there just seems to be something I don’t get in WP. What’s the difference between Action hooks and filter hooks?

    When I look at the source for apply_action in plugin.php, I’m really confused as add_action just calls add_filter. So why the difference?

    function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
    	return add_filter($tag, $function_to_add, $priority, $accepted_args);
    }

    I figured it might be used for backwards compatibility due to legacy code? If that is the case, what should be used today i.e. what is the recommended way to hook functions?

Viewing 3 replies - 1 through 3 (of 3 total)
  • No one there who can clarify this? I’m sure others might be interested in the difference too. Or I’m just plain stupid. That’s another possibility 🙂

    Hi
    I guess you will have figured it out by now, since 2 months have passed.

    Adding a filter is the same as adding an action (hence the code is identical). The difference is in how they are processed once they are called (do_action and apply_filters).

    apply_filters:
    Filters are given a seed, and each successive filter for the same hook processes the result of the previous one.

    do_action:
    Actions work independently of each other. Each one does what they want with the parameters (if any) they are passed.

    As far as I understand it, filters really alter the information, while actions just take the information, “take action”, but don’t actually change it.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Action Hooks versus Filter hooks – I’m confused’ is closed to new replies.
Skip to toolbar