Filter Hooks

Filter hooks do exactly what the word sounds like they do, they remove or change material (filter it).  They take an input and ‘filter’ it through the hooked function which returns the filtered input.  You can hook your function and change the text of a post before it goes into the database or at different hook events throughout the websites execution.

Filter hooks sit at certain execution points either going into the database or being output to the browser screen.  A list of all the available WordPress filter hooks is available here.

The WordPress codex shows the add_filter function as follows,

Here is an example of a filter I used in my plugin L7 Login Customizer.  I use the add_filter function in the constructor of the plugin’s main class, I have to use the

because the function I am hooking is located in the same class.

The function l7wc_logo_link_change is now connected to the login_headerurl filter hook.  Now when the login_headerurl filter hook is executed my filter function will filter the data which in this case is the logo link url and change it to the new url.  There could be more than one filter hooked to the same filter hook.  The ‘priority’ argument decides which filter goes first.  Lower numbers are executed earlier.

The l7wc_logo_link_change function is here,

Notice that this function does not take an argument to filter. This function simply returns the value set in the settings array with the key ‘link_url’.  We could change this function to look like this,

This would return the default WordPress link URL with the word ‘something’ appended to it.  Of course this link would get a 404 but it is a good example of how this works.

For filters you find the filter hook that executes with the data you want to change and hook your function to it. Take the input, do what you want with it, and return it.

~ Layer7web

Leave a Reply