WordPress Hooks

Applies to : Enterprise or Higher version

  • This feature allows you to extend the plugin to your needs.
  • We provide multiple hooks to extend the plugin functionality
  • You can get the access token and perform your own API calls with your OAuth Provider.
    This will allow you to perform multiple tasks such as getting or updating extra user information to and from your OAuth Provider.

List of Hooks

Read Access Token

  • This Hook is called after logging in the user.
  • Calling this hook will give you access to all the parameters returned from the Token Endpoint by the OAuth/OpenID provider.

HOOK: mo_oauth_logged_in_user_token

SAMPLE CODE:

// If your function 'your_function_name' belongs to PHP class                
add_action('mo_oauth_logged_in_user_token', array( $this,'your_function_name'), 10, 2);                

// If your function 'your_function_name' exists outside of PHP class
add_action('mo_oauth_logged_in_user_token', 'your_function_name' , 10, 2);                

// Function Definition
function your_function_name( $user, $token ){                
  // $token : contains all information related to access_token and refresh_token                
  // $user : WP user belonging to above access_token                
}

Get Refresh Token

  • This filter is used to get a new access token using the refresh token.

FILTER: mo_oauth_refresh_token

SAMPLE CODE:

$access_token = apply_filters( 'mo_oauth_refresh_token', $refresh_token, $app_name);

Here, $app_name can be any of the two:

Post Login Redirection

  • This hook is used to redirect the user to the particular url after they are logged in.

HOOK: mo_oauth_redirect_oauth_users

SAMPLE CODE:

// If your function 'your_function_name' belongs to PHP class                
add_action('mo_oauth_redirect_oauth_users', array( $this,'your_function_name'), 10, 2);                

// If your function 'your_function_name' exists outside of PHP class
add_action('mo_oauth_redirect_oauth_users', 'your_function_name' , 10, 2);                

// Function Definition
function your_function_name( $user, $after_login_url ) {
  // $user : Logged in WP user             
  // $after_login_url : contains url to be redirected to after login               
}
  • This hook is used to set a login cookie for a user.

HOOK: mo_oauth_set_login_cookie

SAMPLE CODE:

// If your function 'your_function_name' belongs to PHP class                
add_action('mo_oauth_set_login_cookie', array( $this,'your_function_name'), 10, 0);                

// If your function 'your_function_name' exists outside of PHP class
add_action('mo_oauth_set_login_cookie', 'your_function_name' , 10, 0);                

// Function Definition
function your_function_name() {                
   // your code to setcookie             
}

Get Logged in user attrs

  • This hook is used to get all the user attributes returned by the resource owner.

HOOK: mo_oauth_get_user_attrs

SAMPLE CODE:

// If your function 'your_function_name' belongs to PHP class                
add_action('mo_oauth_get_user_attrs', array( $this,'your_function_name'), 10, 2);                

// If your function 'your_function_name' exists outside of PHP class
add_action('mo_oauth_get_user_attrs', 'your_function_name' , 10, 2);                

// Function Definition
function your_function_name( $user, $resource_owner ) {    
   // $user : WP user 
   // $resource_owner : contains response received from resource owner (user attrs)            
}

Post User Registration

  • This hook is fired immediately after a new user is registered.

HOOK: user_register

SAMPLE CODE:

// If your function 'your_function_name' belongs to PHP class                
add_action('user_register', array( $this,'your_function_name'), 10, 1);                

// If your function 'your_function_name' exists outside of PHP class
add_action('user_register', 'your_function_name' , 10, 1);                

// Function Definition
function your_function_name( $user_id ) {                
   // $user_id : WP user id           
}

Get Form Data

  • This hook is fired while saving settings in the plugin.

NOTE - When using this hook, please make sure you verify the wp_nonce before performing any action.

HOOK: do_main_settings_internal

SAMPLE CODE:

// If your function 'your_function_name' belongs to PHP class                
add_action('do_main_settings_internal', array( $this,'your_function_name'), 10, 1);                

// If your function 'your_function_name' exists outside of PHP class
add_action('do_main_settings_internal', 'your_function_name' , 10, 1);                

// Function Definition
function your_function_name( $post ) {                
  // $post : contains filtered data from the superglobal $_POST.              
}

Change Authorization Request

  • This filter is used to parse and change the Authorization Request (on Authorization Endpoint).

FILTER: mo_auth_url_internal

SAMPLE CODE:

// If your function 'your_function_name' belongs to PHP class                
add_filter('mo_auth_url_internal', array( $this,'your_function_name'), 10, 2);                

// If your function 'your_function_name' exists outside of PHP class
add_filter('mo_auth_url_internal', 'your_function_name' , 10, 2);                

// Function Definition
function your_function_name( $authorization_url, $appname ) {                
   // $authorization_url : Initial authorization URL on which the user will be redirected to (has default parameters attached).               
   // $appname : Name of the application being used.
    return $authorization_url;          
}

Clear Plugin Cache

  • This hook can be used mainly to clear all the configuration/database when the plugin is being uninstalled.

HOOK: mo_clear_plug_cache

SAMPLE CODE:

// If your function 'your_function_name' belongs to PHP class                
add_action('mo_clear_plug_cache', array( $this,'your_function_name'), 10, 0);                

// If your function 'your_function_name' exists outside of PHP class
add_action('mo_clear_plug_cache', 'your_function_name' , 10, 0);                

// Function Definition
function your_function_name() {                
  //your code to perform uninstall action              
}

Customize Error Message

  • This hook can be used to customize your error messages during the SSO flow.

HOOK: mo_tr_login_errors

SAMPLE CODE:

// // If your function 'your_function_name' belongs to PHP class                
add_action('mo_tr_login_errors', array( $this,'your_function_name'), 10, 1);

// If your function 'your_function_name' exists outside of PHP class
add_action('mo_tr_login_errors', 'your_function_name' , 10, 1);

// Function Definition
function your_function_name( $error ){
// $error : default error msg
wp_die($error);
exit;      
}

Change Headers in Userinfo Request

  • This filter is used to add/update to the headers passed to the Userinfo endpoint request.

HOOK: mo_extend_userinfo_params

SAMPLE CODE:

// If your function 'your_function_name' belongs to PHP class                    
add_filter('mo_extend_userinfo_params', array( $this,'your_function_name'), 10, 2);                

// If your function 'your_function_name' exists outside of PHP class
add_filter('mo_extend_userinfo_params', 'your_function_name' , 10, 2);                

// Function Definition
function your_function_name( $headers, $userinfo_url ){ 

     // By default $headers will have following data
     //{‘Authorization’: ‘Bearer <access_token>’}

   // $userinfo_url: userinfo endpoint to be called

    return $headers;               
}

Change Header in Token endpoint request

  • This filter is used to add/update to the headers passed to the Token endpoint request.

HOOK: mo_oauth_coustom_extend_tokenendpoint_params

SAMPLE CODE:


// If your function 'your_function_name' belongs to PHP class
add_filter('mo_oauth_coustom_extend_tokenendpoint_params' , array( $this,'your_function_name'), 10, 1);

// If your function 'your_function_name' exists outside of PHP class
add_filter('mo_oauth_coustom_extend_tokenendpoint_params' , 'your_function_name' , 10, 1);

// Function Definition
  function your_function_name( $headers){  
// By default $headers will have following data 
// {‘Authorization’: ‘Bearer <access_token>’}
// you can add new fields in the header as below
  $headers[‘custom header’] = “ custom value”;
//Where custom header is your header key and custom value is header value

   return $headers;              
}

Change Body Argument in Token endpoint request

  • This filter is used to add/update the body argument passed to the Token endpoint request.

HOOK: mo_oauth_update_token_endpoint_body_parameters

SAMPLE CODE:

// If your function 'your_function_name' belongs to PHP class
add_filter('mo_oauth_update_token_endpoint_body_parameters' , array( $this,'your_function_name'), 10, 1);

// If your function 'your_function_name' exists outside of PHP class
add_filter('mo_oauth_update_token_endpoint_body_parameters' , 'your_function_name' , 10, 1);

// Function Definition
function your_function_name($args){
  return $args;
}

Apply filter on Username before user creation

  • This hook is triggered before user creation and can be used to apply filters on usernames and perform additional checks.

HOOK: mo_oauth_check_user_authorised

SAMPLE CODE:


// If your function 'your_function_name' belongs to PHP class
add_filter('mo_oauth_check_user_authorised' , array( $this,'your_function_name'), 10, 2);

// If your function 'your_function_name' exists outside of PHP class
add_filter('mo_oauth_check_user_authorised' , 'your_function_name' , 10, 2);

// Function Definition
function your_function_name($resource_owner, $username){  
//$ resource_owner is the data coming from IDP.
//$username: will be used for user creation.

     return $username;              
}