A Beginner’s Guide to WordPress Actions & Filters

  • Cubettech
  • Web App Development
  • 9 years ago

A Beginner’s Guide to WordPress Actions & Filters
Wordpress has a number of default functions , Hooks are functions that are used to ‘hook into’ the wordpress functions.Hooks are mainly used while developing plugins.

There are two kinds of hooks

  1. Action hooks
  2. Filter hooks

But for a beginner there might be a confusion as to where to use actions or hooks . The main difference between actions and hooks is that, in Action hooks, developers can create a custom Action to add or remove code from an existing Action by specifying any existing Hook, while in Filter hooks developers can create custom Filters using the Filter API to replace code from an existing Action.

We can dig deep into both Action and Filter hooks via some examples.

  1. Action Hooks

We have many action functions available in wordpress , Explaining each is beyond the scope of this blog , Let’s look into one of the common action hook used called ‘add_action’.

Suppose if we want to send a notification email to a particular mail id when we get a comment for our post , by default, wordpress sends mail to the admin if the comment notification is on . But what if we want to send a mail to the post author , Here we make use of action filters .

 The action hook used for inserting a comment is “wp_insert_comment” , So we need to hook it with a function defined by us.

add_action('wp_insert_comment','sent_comment_mail');

“’sent_comment_mail’”  is a user defined function where we can do our specific task.

And in the function we will add the code to send mail

function sent_comment_mail( $comment_id ) { $email = 'test@gmail.com'; $msg =’Hi you have a new message’; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'To:' . "\r\n"; $headers .= 'From: Easy Blog <no-reply@easyblog.org>' . "\r\n"; wp_mail($email, 'New Comment', $msg, $headers); return $comment_id; }

Our defined function will be called when a mail is sent , This is an example of a wordpress action hook , In simple terms an action hook hooks into a default wordpress function without altering it.

  1. Filter Hooks

Filters can be used to replace a code in an existing function . Let’s explain one of the filter functions called “add_filter”.

The syntax of the function is

add_filter( $existing_filter_name, $function_to_add, $priority );

$priority is used to specify the order in which the functions associated with particular actions are executed .
Now let’s explain this with a simple example, The function ‘sanitize_user’ is used for getting a username stripped with unsafe characters . Now lets use filter function to change the default function to a user defined function called ‘add_sanitize_user’ to add our priorities while sanitizing a username.

add_filter( 'sanitize_user', 'add_sanitize_user');

And later we need to define the new function

function add_sanitize_user($username, $raw_username) { $new_username = strip_tags($raw_username); $new_username = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '', $new_username); $new_username = preg_replace('/&.?;/', '', $new_username); return $new_username; }

In some cases ,we may need to disable hooks later , and then we could make use of remove hooks functions .You can do that by calling remove_filter(‘filter_hook’,’filter_function’) or remove_action(‘action_hook’,’action_function’).

These are some simple yet amazing features of WordPress to make it the most developer friendly content management system in the world . At this point we recommend you to go through WordPress codex articles to get a more advanced knowledge on hooks.

Know More About This Topic from our Techies

Got a similar project idea?

Connect with us & let’s start the journey!

Questions about our products and services?

We're here to support you.

Staff augmentation is a flexible workforce strategy companies adopt to meet specific project needs or address skill gaps.

Begin your journey!
Need more help?