How to Use remove_action in WordPress: Syntax and Examples
In WordPress, use
remove_action to stop a function hooked to an action from running. It requires the action name, the function name, and optionally the priority to correctly remove the hooked function.Syntax
The remove_action function has three parameters:
- action_name: The name of the action hook you want to remove a function from.
- function_name: The name of the function you want to remove.
- priority (optional): The priority level the function was added with; defaults to 10 if not specified.
All parameters are required to match exactly how the function was added with add_action.
php
remove_action( string $tag, callable $function_to_remove, int $priority = 10 ): bool
Example
This example shows how to remove a function hooked to the wp_footer action. The function custom_footer_text is first added, then removed so it does not run.
php
<?php function custom_footer_text() { echo '<p>Custom footer text here.</p>'; } // Add the function to wp_footer add_action('wp_footer', 'custom_footer_text'); // Remove the function from wp_footer remove_action('wp_footer', 'custom_footer_text'); ?>
Common Pitfalls
Common mistakes when using remove_action include:
- Not matching the
priorityused inadd_action. If the priority differs, the function won't be removed. - Trying to remove anonymous functions or closures, which cannot be removed because they have no name.
- Calling
remove_actiontoo early before the originaladd_actionruns, so the hook is not yet registered.
php
<?php // Wrong: priority mismatch, remove_action will fail add_action('init', 'my_init_function', 15); remove_action('init', 'my_init_function', 10); // Does not remove // Right: matching priority remove_action('init', 'my_init_function', 15); // Wrong: trying to remove anonymous function add_action('wp_head', function() { echo 'Hello'; }); remove_action('wp_head', function() { echo 'Hello'; }); // Does not remove ?>
Quick Reference
Tips for using remove_action effectively:
- Always use the exact same function name and priority as
add_action. - Use named functions instead of anonymous functions if you plan to remove them.
- Call
remove_actionafter the originaladd_actionhas run, often inafter_setup_themeorinithooks.
Key Takeaways
Use remove_action with the exact action name, function name, and priority to disable hooked functions.
Anonymous functions cannot be removed with remove_action; use named functions instead.
Call remove_action after the original add_action runs to ensure the hook exists.
Matching the priority parameter is crucial for remove_action to work correctly.
remove_action helps customize WordPress behavior by disabling unwanted hooked functions.