0

this is a very general question, which I have really researched. Maybe I'm just being stupid, but I can't quite get when I can use a filter. After some messing around earlier, it seems I can take any function in the core files, pass this as the filter name, and the second argument I can replace this with my own function in the functions.php or plugin file. Is that correct? Can I literally modify anything doing this?

I've been trying to alter some fields on the user profile page, which sucks because some can be altered and some can't as the PHP and HTML doesn't come from a function or class I can work with - it seems certain things can only be changed by hacking around the core files, which I'm not about to do!

So, in summary, can anyone please give me a very short and sweet explanation as to why and when I'd use an action or a filter. This has become I hurdle I wan't to get over! Many thanks ,Dan.

Update: Sample code?

function get_stylesheet_directory() {
    $stylesheet = get_stylesheet();
    $theme_root = get_theme_root( $stylesheet );
    $stylesheet_dir = "$theme_root/$stylesheet";

    return apply_filters( 'stylesheet_directory', $stylesheet_dir, $stylesheet, $theme_root );
}
1
  • You should ask a question about the fields you are trying to change before giving up. Some of us are quite inventive. Aug 24, 2013 at 0:12

2 Answers 2

3

Filters and Actions are similar under the hood, but with an important difference-

Actions allow you to do something when an event occurs- WordPress does something and executes an action to let you do your own thing. A function hooked to an action may or may not produce output, but it never returns a value

Filters allow you to replace or modify some kind of data. WordPress has some data it is using in a function, but before it is used, it applies a filter to that value to let you change it. It's generally safe to say that a filter will return something.

I can take any function in the core files, pass this as the filter name, and the second argument I can replace this with my own function in the functions.php or plugin file.

You can't arbitrarily select a function name and add a filter to it, though as you've discovered, this may seem to be the case because WordPress has conveniently named many actions to match the function names that contain those actions.

You can only add a filter where the code contains a call to apply_filters hooked to that tag.

You can only add an action where the code contains a call to do_actions hooked to that tag.

3
  • "You can only add a filter where the code contains a call to apply_filters hooked to that tag." - so I need to look through the core files to identify where this is possible? I'm aware of Adam Browns site, with the huge list of hooks, but knowing when and why to use them is another matter. I've updated my question with some code from theme.php, is this is sort of thing I'd look for?
    – Dan
    Aug 23, 2013 at 18:49
  • Yes, your example is what you should be looking for to identify where a filter can be used. The Codex has a decent list as well, but if you want a completely exhaustive list of all of them and a firm grasp of how they are used, it's probably going to involve looking through source. This is where having a good IDE or text editor is crucial.
    – Milo
    Aug 24, 2013 at 4:31
  • Ok, this is making more sense now. How about the implementation of add_filter ,does this fire every time the function that you filter is called? I would also assume apply_filter and apply_filters are different because they filter one or several values respectively?
    – Dan
    Aug 24, 2013 at 7:55
1

The rule of thumb that I follow is:

  • Use add_filter() when you want to modify some text that will be output (outputted?) to the user's screen. So you can filter things like the_content, the_title, wp_title, and so forth.
  • Use add_action() when you want to perform some other task -- for instance, echo()ing extra text, modifying a global variable, changing a query, etc.

Note that the Codex's lists of filters and action hooks, while a good starting point, is far from complete. You may want to search at sites like http://wpseek.com/ if you need something more esoteric.

1
  • Thank you for the link to wpseek.com, that looks very, very useful. Also, thanks for writing.
    – Dan
    Aug 23, 2013 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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