Hooks: Actions and Filters in WordPress

Hooks in WordPress plays a vital role in the development cycle of any application in WordPress. Hooks allows the users to simply tie your custom code to the rest of the WordPress core base. In this article we will be looking onto what exactly are the Hooks in WordPress and some examples on using them.

A hook is generally referred as a process of attaching a component or a function that modifies the default functionality of WordPress or vice-versa. So, Basically, there are two types of hooks in WordPress.

  1. Actions
  2. Filters

Action hook are generally triggered by specific events that take place in WordPress, such as publishing an article, updating, or even initializing your custom code that is hooked into a certain action.

Similarly, Filters are filters. It filters the desired content passed accordingly. It acts as a middleware between the database and the browser and vice-versa. Some example would be to filter out a content by customizing the content on how it is displayed.

In simple terms Action is used in execution level. That means action is used when you want to run a certain piece of code in the WP runtime. This not modifying or receiving piece of data. Similarly, Filters allow you to fetch a piece of data and then return it back using custom functions.

Adding Actions and Filters

For action hooks it is pretty straight forward. The process is quite simple. In order to execute an action you will need a piece of information which the action can process. The piece of information will be our function. We hook that function by following.

Hooking Actions

Hook_name and Function name are the required parameters here. Others are optional. The priority is based on a integer scale of 1 to 9999 that determines the priority order of functions those are tied to that specified hook. Default is 10. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order added to the action. Accepted Arguments are an optional integer argument defining how many arguments your function can accept (default 1), useful because some hooks can pass more than one argument to your function.

Hooking Filter

The add_filter works same as add_action. A filter function takes as input the unmodified data, and returns modified data (or in some cases, a null value to indicate the data should be deleted or disregarded). If the data is not modified by your filter, then the original data must be returned so that subsequent plugins can continue to modify the value if necessary.

Removing Actions and Filters

To remove a action or a filter is very simple. Use the remove_action or remove_filter for removing them with the name of the hook, function and priority. Note that if a hook was registered using a priority other than the default of 10, then you must also specify the priority in the call to remove_action(). Also note that in general, you shouldn’t remove anything unless you know what it does and why it does it — check the WordPress or other plugin source code to be sure.

Examples of Action Hook

WordPress has following actions available in the Action Reference Page.

Removing Certain Admin Menus

In the example above you can see that remove_certain menus is being hooked to admin_init action hook. So, this code runs when the admin is initialized. The action will remove certain menus from admin.

Hooking Script into Footer

The above code will add the scripts into the html footer in admin section.

Examples of Filter Hook

WordPress has following filters available in the Filter Reference Page.

Changing the Excerpt length

In the above code we used excerpt_length filter which gives us the determined integer length from the_excerpt and then filter that with our return value from change_length_excerpt function.

See more on Hooks

This article is basic guidelines for using hooks in WordPress. You can get complete reference from WordPress Codex. Action References are here. Filter references are here.

ABOUT THE AUTHOR

In a world without walls and fences, who needs windows and gates ? No gates, no windows bt apache inside! #dev Love for #Unix #Linux #Apple. Interests in coding, music, creative design, Linux and Apple products.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.