- by codepopular
- 0 Comments
- WordPress
- Views 402
Hooks
Hooks are a way for one piece of code to interact/modify another piece of code at specific, pre-defined spots. They make up the foundation for how plugins and themes interact with WordPress Core, but they’re also used extensively by Core itself.
There are two types of hooks: Actions and Filters. To use either, you need to write a custom function known as a Callback
, and then register it with a WordPress hook for a specific action or filter.
Actions allow you to add data or change how WordPress operates. Actions will run at a specific point in the execution of WordPress Core, plugins, and themes. Callback functions for Actions can perform some kind of a task, like echoing output to the user or inserting something into the database. Callback functions for an Action do not return anything back to the calling Action hook.
Filters give you the ability to change data during the execution of WordPress Core, plugins, and themes. Callback functions for Filters will accept a variable, modify it, and return it. They are meant to work in an isolated manner, and should never have side effects such as affecting global variables and output. Filters expect to have something returned back to them.
WordPress provides many hooks that you can use, but you can also create your own so that other developers can extend and modify your plugin or theme.
Actions vs. Filters
Actions:
- Execution Time: Actions are executed at a specific point during the execution of WordPress, often in response to an event or user interaction. They are like “do this now.”
- Output: Actions allow you to add, modify, or perform custom tasks without altering the data directly. They can be used to add content, execute functions, or perform actions, typically affecting the output of a page.
3. Return Value: Actions do not return a value. They are designed for executing code or functions.
Filters:
- Execution Time: Filters are executed at a specific point, just like actions, but they have a primary focus on manipulating and modifying data before it is displayed or used. Filters are like “let me change this data before it’s shown.”
- Output: Filters allow you to modify the data that is being processed, such as post content, titles, widgets, etc., before it’s displayed. They are used for altering the output.
- Return Value: Filters return a modified value, allowing you to change or filter the data being processed.
We have a WordPress plugin where you can enhance your upload size. its easy to install and setup in your website. if you want to learn more about it please read the blog here
Watch video to learn difference between action and filter in WordPress
Here’s a practical example to illustrate the difference between actions and filters:
Action Example: Suppose you want to add a custom message before the content of each post. You would use an action for this:
Filter Example: Now, suppose you want to add a custom prefix to the post title. You would use a filter for this:
Hi, My name is Shamim. I am a freelance PHP developer in Bangladesh. I have been working as a freelance developer since 2014. I am a passionate and creative web development person. As a senior level, I focus on your requirements in detail and deliver high-quality work on your budget.