WordPress provide functionality of action hook with which you can create your own action hook too.you can create new action hooks by calling do_action() function and just need to specify the name of the new hook as $tag parameter.Whereas add_action() is used to hooks a function on to a specific action.
SYNTAX for do_action
1 2 3 | do_action( $tag, $arg ); |
Parameters
$tag is the name of the action to be executed.
$arg is an optional argument which are passed on to the functions hooked to the action.
SYNTAX for add_action
1 2 3 | add_action( $tag, $function_to_add, $priority, $accepted_args ); |
Here is add_action, same like do_action $tag is the name of the action to be executed.Next argument is the name of the function to add for an action.The third argument defines the priority of code and the fourth argument is for a number of argument in the function.
Here in an example, going to show how to create an action hook that customize the login functionality just before the user logged into the dashboard.
First of,place below code into the wp-login.php file after post request for a form.
1 2 3 | do_action('login_qry',$user); |
Next is use the function, which we can put in the functions.php of the theme to add an action for the login
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function login_qry_fn($user){ global $wpdb; $userid =$user->ID; $result = $wpdb->get_var("Your_qry"); if(empty($result)){ wp_redirect( site_url('wp-login.php') ); exit; }else{ return true; } } add_action('login_qry','login_qry_fn',10, 1); |
Thanks for reading and feel free to share your thoughts! Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.
Comments (5)