Browsing Tag

filter

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.

WordPress Tips

Add link to title of widget text, or any widgets

December 28, 2016

Sometimes you want to add link to the Widget Title. Using “widget_title” filter you want do that. Below is a simple way to do that. Let’s start with the code

add_filter('widget_title', 'my_widget_title_filter', 10, 3);
function my_widget_title_filter( $title, $instance, $id_base){
if($id_base == 'text'){
// so we only want to apply on Text Widget
if($title == 'Widget Text'){
$title = '<a href="#">' . $title . '</a>';
}
}

return $title;
}

So the “widget_title” filter accepts 3 parameters. The first one is the current title, the second one contains all parameters of the widget. The last one helps us to recognize what type of widget it is. So, we check

$id_base == 'text'

to make sure you only want to apply on Text Widget. Actually this check is not necessary if you don’t care about this. Next, we check which title should be added link. This will be tricky if you have different widgets having same title. Make sure widgets have different titles, so the link only be added to one widget.

Finally, we add link to the title. This solution is not perfect, but simple and fast enough to be used.