WordPress comes with very awesome concept of Hook which allows you to extend, enhance, customize the existing / core feature of WordPress according to your requirement and without touching the core of the WordPress code.
Yes, its possible with the use of Hooks. Hooks are of 2 types. Filters and Actions.
Filters are used to filter the content that is only for outputting the content to the browser. Where as Action is used to perform an action when something happens like publish post, login, delete a post etc. Its basically event driven programming for Action.
Lets understand how to use Filters and Actions in your WordPress code. All you need to know about these hooks of WordPress. Lets Start!
- How to call Filter?
You can apply function as above. As the parameter suggest 1st argument is for name of the filter, then the name of the function which we want to call and then passing parameters to the calling functions.
Lets understand by Example.
Suppose we want to add a “Sankhala Info Solutions” at the end of each description/content of the post.
$strContent = apply_filters('the_content','fnAddMyCompanyName');
return $strContent.' Sankhala Info Solutions';
The above code base will help you to add your company name at the end of the content. You can modify the data, truncate some chars etc with the use of Filters.
- How to create custom Filter?
the_content is the in built filter available. What if if you want to create your own filter. Its lot of time required when you are developing a plugin especially and wants the community to extend, enhance the behavior of your plugins.
- How to remove existing Filter?
Sometimes it is required to remove unwanted content OR some plugins overwrite your content OR does not allow you to finish the task that client requires. Then you have to remove the filter.
Here is the way you can remove the filter.
remove_filter('name_of_filter','name_of_function', 'priority' );
Action is also very useful when you need to store more info in a separate table for one purpose or the other. This is the basic requirement and mainly applicable in Admin area of WordPress while developing a solution for client.
Lets understand how to create, remove, fire an Action in WordPress.
- How to call custom function through Action?
Whenever some events happens we can call our custom function with the use of Actions. For calling our custom function, we can use the following.
As the function says above, 1st Argument Name of Action, 2nd Argument function to be called when Action was invoked, Priority when our custom function should be called, Number of args.
- How to create Custom Actions?
For custom coding, plugin development we may require an action to be created to allow community to add their enhancement for the plugin and make it usable for their need.
To do, so we can define custom action. Here follows how we can do this.
As you can see in the above function, we can define action with “do_action” function. The 1st argument is the name of the action and 2nd Argument is of number of argument to be passed.
- How to remove Action?
Sometimes it happens that we need to revoke the action for particular page in question. We can do so with the use of following function.
Hope you liked the article and it will help you to work with filters and actions easily.