WordPress – Visual Composer (VC) shortcode cleaning article, I partially mentioned the detailed explanation regarding add_filter and add_action is now timely.
Therefore, let’s quickly summarize our introduction to the topic.
WordPress Hooks
We can modify theme code to implement JavaScript and CSS changes and partial enhancements in WordPress web pages. However, when dealing with changing the behavior of WordPress, themes, or plugins, front-end and/or functions.php modifications will be limited. At this stage, we leverage WordPress Hook structures. We can either present these structures as a plugin or simply allow processing through functions.php1.
We can use the term hook as a “hook” (a “catch”). In the context of the Plugin API, the term “hooks” commonly refers to the interaction between one code operation and another, or enhancing/modifying an existing operation2 3. The term “hook” is used because existing operations are already being executed by WordPress. Using hook expressions introduced in WordPress 2.1, we can create additional operations that integrate with the execution of WordPress processes. In other words, when an operation is performed on an action and/or filter, the related function must also be triggered for the operation to be executed.
Let me illustrate this with a simple example:
add_filter( 'the_content', 'removeVCShortcodes' );
function removeVCShortcodes( $currentContent ) {
$cleanContent = preg_replace('/\[(.*?)\]/', '', $currentContent );
return $cleanContent;
}
The above add_filter operation interferes with the the_content() function. Therefore, it applies to areas where this function is used. Whether or not there is a corresponding counterpart in the function’s content is not important. In other words, we created a regex pattern inside the function, and this pattern filters Visual Composer tags. If the content does not use any tags belonging to the plugin, the pattern preg_replace('/\[(.*?)\]/', '', $currentContent) will not perform any operation. If instead of the_content() we had used publish_post(), the operation would have been applied at the stage when the content is saved to the database. Let’s continue explaining the hook structure. WordPress Core performs many operations that are linked to content. Hooks form the foundation of how plugins and themes interact with the WordPress Core. However, the Core itself also commonly uses hook structures.
Action (add_action()) and Filter (add_filter())
WordPress supports two types of hook usage: actions (events) and filters (filters). You might occasionally encounter situations where hooks are used outside of actions and filters4. However, the essence is that you should understand that WordPress Core is involved, and that hooks directly or indirectly perform operations on this core. For both actions and filters to be usable, a special function known as a callback must be attached to them via another function. For example:
add_filter( 'the_content', 'function-name-1' );
function function-name-1(){
/* ... */
}
add_action( 'woocommerce_thankyou', 'function-name-2' );
function function-name-2(){
/* ... */
}
IMPORTANT RULES:
- Maintain the original formatting (markdown, HTML tags, links, etc.)
- Keep technical terms and proper nouns as appropriate
- Preserve code blocks and technical syntax exactly
- Maintain the same tone and style
- Only output the translated text, no explanations or comments
The first function, function-name-1(), is being called from within add_filter(), and as a result, the operation applied within the function (filtering) is then applied to the_content(). In another example, function-name-2() is associated with the woocommerce_thankyou action. We previously used this example in WooCommerce – Facebook conversion code definition to trigger the conversion code on the WooCommerce payment confirmation page.
add_action()
Action (add_action) allows us to add data or intervene in how WordPress processes work. Callback functions execute at a specific point during WordPress operations and can perform specific tasks such as returning output (themes and plugins) and/or database modifications5.
add_action( $tag, $function_to_add, $priority = 10, $accepted_args = 1 );
$tagthe function to be filtered or extended (string)$function_to_addthe function created for filtering or extending (callable)$prioritythe priority of the filter execution (int). Defaults to 10 if left blank.$accepted_argsused for additional values required in shortcodes and/or for the function (int). Defaults to 1 if left blank.
At least $tag and $function_to_add must be provided when using add_action(). To define a function associated with the init action, the following callback function would be most basic:
add_action('init', 'newCallbackFunction');
function newCallbackFunction(){
// operations to be performed
}
Available functions are as follows:
| Function | Description |
|---|---|
has_action() | Checks whether any hook definition has been made. |
add_action() | Allows a hook function to be processed. |
do_action() | Enables us to associate multiple functions with a single hook. |
do_action_ref_array() | Allows us to pass arguments as an array (list) in addition to do_action(). |
did_action() | Returns information about how many times a specific hook has been executed. |
remove_action() | Removes a function associated with a specific action hook. |
remove_all_actions() | Removes all other functions associated with a specific action hook. |
doing_action() | Allows us to check or determine whether an action hook is currently being processed. |
add_filter()
Filter (add_filter) provides the ability to modify data during WordPress execution. A filter callback function accepts a variable, modifies it, and returns it. All these operations are isolated and never affect global variables or output. Usage is as follows:
add_filter( $tag, $function_to_add, $priority, $accepted_args );
$tagthe function to be filtered and extended (string)$function_to_addthe function created for filtering or extending (callable)$prioritythe priority of the filter execution (int). Defaults to 10 if left blank.$accepted_argsused for additional values required for shortcodes and/or functions (int). Defaults to 1 if left blank.
Available functions are as follows:
| Function | Description |
|---|---|
has_filter() | Checks whether a filter has been defined as a hook. |
add_filter() | Allows a function or method to be processed as a filter hook. |
apply_filters() | Calls functions attached to a filter hook. |
apply_filters_ref_array() | Allows processing of functions attached to a specific filter hook as an array. |
current_filter() | Returns the currently active filter hook. |
remove_filter() | Removes a function associated with a specific filter hook. |
remove_all_filters() | Removes all functions associated with a specific filter hook. |
doing_filter() | Provides a way to check whether a filter hook is currently being processed. |
Function Activation/Deactivation/Removal
register_activation_hook()register_uninstall_hook()register_deactivation_hook()
I tried to explain actions and filters hooks as simply as possible, using basic information and features. I will continue to add more details over time. In future posts, I will frequently cover the relevant functions and usage examples, which I believe will make the processes clearer. Additionally, if you have any questions or issues, please feel free to comment or message me. Below, I’ve also listed a few additional resources that I believe will be helpful.