Forum Moderators: rogerd & travelin cat

Message Too Old, No Replies

Need Help Understanding Hooks, Actions And Filters, Please.

         

Planet13

7:52 am on Dec 16, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



OK, I have been reading up on hooks, actions and filters, and I THINK that I got my head around it.

Maybe you can read through and just correct any misunderstandings I might have:

Please clarify: True or False:

1) Hooks are available in two flavors: Either action hooks, or filter hooks.

2) Hooks are defined by the core wordpress theme and the child theme / framework.

3) You will either have to look in the wordpress codex, or in the template files themselves, to tell if a hook is an action hook or a filter hook.

4) An action hook allows you to add an action by using the following sytnax:

add_action ( [the name of the hook], [the name of the function being called], [Optional: Priority Level with default of 10 if undeclared], [Optional: the number of arguments with default of 1 if undeclared]);

5) The function that is called can appear either before OR after the add_action hook in the functions.php file. (Or does it need to appear in the functions.php file BEFORE the add_action that calls it?)

6) The function that is called by add_action MUST end with a return $args; which matches the $args when the function is first declared.

7) Filter hooks will have apply_filters appear in front of them where they appear in the templates.

8) Often hooks will have the same names as core wordpress functions

Please let me know whether these assumptions are right or wrong. And if you have any other tips on understanding them - in particular, understanding FILTER hooks - they are greatly appreciated.

Thanks.

lorax

1:46 pm on Dec 16, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm no expert on them but I think you're pretty close.

Hooks aren't used by WordPress core files. They exist to allow theme and plugin authors a way to modify how WordPress works without having to edit the core files.

Yes, there are two types of hooks:
  • Action (add & remove)
  • Filter (add & remove)

    A great resource to view the available hooks is here: [adambrown.info...]

    If there IS a way to test to see if an Action is in use or not I'm not aware of it. Your syntax for the add_action is correct. The function can come before or after the add_action line.

    The function being called by a hook must return something. That's why it exists - to process, create, alter or filter something to be used by the theme or plugin.

    I'm not sure filters have to use apply_fiter. This page of the CODEX might more info (see the Related section at bottom): [codex.wordpress.org...]

    Since hooks are designed to alter the way WordPress functions, I would expect them to have similar names as the core functions - would help keep things clear.

    But maybe someone with more experience will chime in.
  • Planet13

    3:34 pm on Dec 16, 2014 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    Thank you, Lorax.

    Ok, let me ask a few more GENERAL questions.

    I know I will have to look up if there are any hooks there already, but I would assume that I would need to find the correct filter hooks if:

    1) I wanted to remove the URL field from the comments form.

    2) I wanted to strip any URL data from the comments form that might otherwise get written to the database.

    3) I wanted to remove any INLINE CSS that has the !important priority attached to it.

    Just looking for confirmation that these are the types of scenarios that would normally use FILTER hooks instead of action hooks (since we would basically be removing one piece of data that is already being displayed or written to the DB).

    Or would it be more appropriate to use action hooks in these situations?

    Thanks in advance.