Wordpress Developer

Action & Filter in WordPress

March 7, 2018

What’s Action hook ?

In WordPress; an Action is a PHP function that is executed at specific points throughout the WordPress Core.

Developers can create a custom Action using the Action API to add or remove code from an existing Action by specifying any existing Hook. This process is called “hooking”.

add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 );

Parameters:
$tag
(string) (Required) The name of the action to which the $function_to_add is hooked.

$function_to_add
(callable) (Required) The name of the function you wish to be called.

$priority
(int) (Optional) Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.

Default value: 10

$accepted_args
(int) (Optional) The number of arguments the function accepts.

Default value: 1

Return
(true) Will always return true.

For example with existing action: A developer may want to add code to the footer of a Theme. This could be accomplished by writing new function, then Hooking it to the wp_footer Action.

<?php
function your_function() {
echo '<p>This is inserted at the bottom</p>';
}
add_action( 'wp_footer', 'your_function' );
?>

For example with custom action: do_action creates an action hook, add_action executes hooked functions when that hook is called.

do_action( 'my_footer_hook' );

Which you can use in the callback function
add_action( 'my_footer_hook', 'my_footer_echo' );
function my_footer_echo(){
echo 'hello world';
}

 

What’s Filter hook ?

WordPress offers filter hooks to allow plugins to modify various types of internal data at runtime.

A plugin can modify data by binding a callback to a filter hook. When the filter is later applied, each bound callback is run in order of priority, and given the opportunity to modify a value by returning a new value.

add_filter( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 );

Parameters
$tag
(string) (Required) The name of the filter to hook the $function_to_add callback to.

$function_to_add
(callable) (Required) The callback to be run when the filter is applied.

$priority
(int) (Optional) Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.

Default value: 10

$accepted_args
(int) (Optional) The number of arguments the function accepts.

Default value: 1

Return
(true)

 

For example with existing filter: the_content

<?php
function cactus_content_filter( $content ) {
$find = 'hello';
$replacement = "<strong>hello</strong>";
$content = str_replace( $find, $replacement, $content );
return $content;
}
?>

<?php
add_filter( 'the_content', 'cactus_content_filter' );
?>

WordPress plugin API has an extensive list of filter hooks available in WordPress.

For example with custom filter:

<?php
$copyright = 'Design by CactusThemes';
echo apply_filters( 'cactus_copyright', $copyright );
?>

Result: Design by CactusThemes.

<?php
function cactus_copyright_filter( $content ) {
$content = 'Copyright by CactusThemes'
return $content;
}
?>

<?php
add_filter( 'cactus_copyright', 'cactus_copyright_filter' );
?>

Result: Copyright by CactusThemes.

All the above examples are working examples. If you have a test installation of WordPress to play with, you can try using the code above in the functions.php in your child theme.

You Might Also Like