WordPress is a powerful tool and there is no doubt about that. Another important aspect to note is that it takes minutes to learn and years to master. Even old timers who have been working on the platform for quite a while, are still learning new things as they come across new projects.
Everyone who is into WordPress development, makes friends with the hooks as they cannot ignore them for long. There comes a time when they have to delve into them and head on. Of Course, making changes in the WordPress code files is not recommended, so whenever one needs to change an existing functionality or create an entirely new functionality, they will have to look to adopt hooks.
Why shouldn’t you edit the core
The occurrence of “core edits” is quite often. Usually they are small modifications and changes, maybe you need extra information returned from a method. Or you don’t want to make another function call. You must not forget that when the core is updated eventually, those changes will be wiped away and it will break whatever feature was built around the modified core.
That “small” little hack can be an issue for the client and any developer who is handling the project then.
Here are some hooks that can be absolutely beneficial in your WordPress journey : Whether you are developing plugins or themes for a client or mass distribution.
Let us go over a few definitions
Hook: In WordPress, a hook is a generic term. I refers to a place where one can add their own code change whatever WordPress is doing or outputting by default. There are two types of hooks in WordPress: Actions and Filters
Action: An action, in WordPress is a type of hook that lets you take an action and is triggered at a specific time when WordPress is running. These actions can be stuff like creating a widget when WordPress initializes or sending a tweet when a post is published.
Filter: Using a filter, you can get and modify WordPress data before it is sent to the database or the browser. An example of filter is customizing how excerpts are displayed or adding some tailored code to the end of a blog post. Basically they allow you to edit content before it’s output.
To understand, let’s look at the core filter functioncapital_P_dangit():
function capital_P_dangit( $text ) { // Simple replacement for titles $current_filter = current_filter(); if ( 'the_title' === $current_filter || 'wp_title' === $current_filter ) return str_replace( 'Wordpress', 'WordPress', $text ); // Still here? Use the more judicious replacement static $dblq = false; if ( false === $dblq ) { $dblq = _x( '“', 'opening curly double quote' ); } return str_replace( array( ' WordPress', '‘Wordpress', $dblq . 'Wordpress', '>Wordpress', '(WordPress' ), array( ' WordPress', '‘WordPress', $dblq . 'WordPress', '>WordPress', '(WordPress' ), $text ); }
This filter picks up the input text, scans it for instances of “WordPress”, and fixes the text returning a filtered version of the string it was passed and nothing more.
WordPress has quite good APIs for hooks and filters
Some WordPress developers may face a confusion in figuring out whether a hook is an action or a filter. The part about filters is that as you are working with it, you receive some data and at the end of your function, you return it back.
Contrarily, in an action, one is not receiving and modifying data. They are simply given a place in the WordPress runtime where a code can be executed.
Adding and removing your own functions
The process for hooking into your own functions is quite simple. You must first know some information. In case of actions, you should know the name of the hook and when it runs. In case of filters, apart from knowing the name of the hook, you must know what value you will get and have to return. Finally, you also need to know the name of the function where you have all your code.
The workflow of using hooks
Though hooks are better documented these days, untill recently they were neglected quite a bit. The best way to use them is to use Adam Brown’s hook reference or look at the source code.
The beauty of mix and match
The best part about it is that functions are used for everything. The same profanity filter can be used for other purposes (even outside of WordPress) if you want, because it is just a function. These functions are easily reusable in various situations, giving the developer more flexibility and saving you some time.
Conclusion
This was a very primitive insight into working with hooks in WordPress. Hopefully, now you are aware of how this system works. The important pattern explained here could be seen even outside of WordPress. It however, takes some time in getting used to, especially if you are entering it without any prior knowledge and know-how. The major issue is that coders get lost in all the filters available or in finding their arguments and so on, however it can be overcome with some patience. Once you start using them, you can be a master in a short time.
Related
WordPress Essentials: The Definitive Guide To WordPress Hooks
WordPress Hooks: Actions, Filters, and Examples
WordPress Actions, Filters, and Hooks : A guide for non-developers
Best Freelance WordPress Developers for Hire
Finally, I understood the concept of hooks and filters. You have described this concept in very simple language. I work for an wordpress development firm and hence I was very curious to know about hooks as this can help me in many ways. Many times I have googled this concept but not found any useful info like this. Thanks for writing this useful article. It was worth reading.