Login Hooks

Google Apps Login hooks


Action: gal_user_loggedin

This action is fired whenever a user is successfully authenticated into WordPress using Google Login. You could use it to obtain and save information such as profile photo URL.

Params:

$wp_user – the WP_User object that just logged into WordPress (via Google auth) – note this could have been created by the plugin just now
$google_userinfo – an object holding the raw profile data direct from Google
$wp_userisnew – boolean set to true if the WP_User has just been created on this login; false if the WP user already existed
$google_client is a Google client library object that you could use to make further Google calls
$google_oauth2service – a Google object used to fetch the profile data for GAL plugin

Return:

Void (nothing)

Example:

function my_google_user_loggedin( $wp_user, $google_userinfo, $wp_userisnew, $google_client, $google_oauth2service ) {
   /* google_userinfo object example:
   "id": "115886881859296909934",
   "email": "[email protected]",
   "verified_email": true,
   "name": "Dan Lester",
   "given_name": "Dan",
   "family_name": "Lester",
   "link": "https://plus.google.com/115886881859296909934",
   "picture": "https://lh3.googleusercontent.com/-r4WThnaSX8o/AAAAAAAAAAI/AAAAAAAAABE/pEJQwH5wyqM/photo.jpg",
   "gender": "male",
   "locale": "en-GB",
   "hd": "mycompany.com"
   */

   // Do something with the data
   error_log("User just logged in with WordPress account ".$wp_user->ID." bringing Google user id ".$google_userinfo->id);
   error_log("User is ".( $wp_userisnew ? "brand new" : "old"));

   // Save picture URL
   update_user_meta( $wp_user->ID, 'user_avatar', $google_userinfo->picture );
}

add_action('gal_user_loggedin', 'my_google_user_loggedin', 10, 5);

Filter: gal_login_url – versions 2.8.11+

The Login plugin applies this filter to the login URL it calculates for your site, allowing you to change the ‘callback’ URL that it passes to Google’s login flow. Normally that is /wp-login.php on your site. You might want to change it if you find that the plugin determines an incorrect URL as the callback – for example if another custom login plugin tries to convince all other plugins that its own custom login page (perhaps at /login) is the real URL.

Params:

$login_url – the login URL that the plugin has calculated.

Return:

String – the URL that you want to use to represent the login page instead. Return $login_url for no change.

Example:

function my_gal_login_url($login_url) {
   return 'https://mysite.com/login';
}

add_filter('gal_login_url', 'my_gal_login_url', 10, 1);

Filter: gal_login_button_text – versions 2.8.17+

Filter gal_login_button_text allows you to specify the text displayed on the ‘Login with Google’ button displayed on the WordPress login page. This will work in all versions of the plugin, but in Premium/Enterprise versions it is also possible for an admin to set the button text in Settings, and that will always take priority if set.

Params:

$login_button_text – the default button text.

Return:

String – the button text that you want to use instead. Return $login_button_text for no change.

Example:

function my_gal_login_button_text($login_button_text) {
   return 'Click Here to Login';
}

add_filter('gal_login_button_text', 'my_gal_login_button_text', 10, 1);

Filter gal_set_login_cookie allows you to specify whether the plugin should set the wordpress_google_apps_login cookie on the current request. By default, the cookie is set on any request if it doesn’t already exist. However, it is normally only absolutely necessary to update this on requests to the wp-login.php page in most setups.

The default scenario could affect caching behavior for some web servers, potentially causing all pages to be generated dynamically every time.

Params:

$dosetcookie – whether to set the cookie or not (boolean).

Return:

$dosetcookie – boolean value whether to set cookie or not.

Example:

function my_gal_set_login_cookie($dosetcookie) {
  // Only set cookie on wp-login.php page
  return $GLOBALS['pagenow'] == 'wp-login.php';
}
add_filter('gal_set_login_cookie', 'my_gal_set_login_cookie');

Filter: gal_sanitize_username – Enterprise/Premium plugin 2.8.6+ only

The Login plugin applies this filter to allow your extension to modify the username of an automatically created user when they first attempt to Login with Google.

Params:

$username – The default username which will normally be the email address of the user
$userinfo – An array containing details from the user’s Google profile (e.g. name, given_name, family_name)

Return:

String – the username that you would like for the user. This should be a unique username in your WordPress site.

Example:

function my_gal_sanitize_username($username, $userinfo) {
  $parts = explode("@", $username);
  if (count($parts) == 2) {
     $username = $parts[0]; /* Just return the bit before the domain name */
                            /*- e.g. [email protected] becomes dan.lester */
  }
  return $username;
}

add_filter('gal_sanitize_username', 'my_gal_sanitize_username', 10, 2);

Note: please see the gal_pre_create_new_user to modify more fields than just the username.


Filter: gal_pre_create_new_user – Enterprise/Premium plugin 2.9.6+ only

The Login plugin applies this filter to allow your extension to modify the WordPress userdata of an automatically created user when they first attempt to Login with Google. This means the data that is passed to the WordPress function wp_insert_user to create the user.

Params:

$wpuserdata – The userdata to be passed to wp_insert_user. The gal_sanitize_username filter will have already been applied.
$userinfo – An array containing details from the user’s Google profile (e.g. name, given_name, family_name)

Return:

$wpuserdata – the userdata to be used to create the WordPress user, or a WP_Error object to abort creation of the user.

Example:

function my_gal_pre_create_new_user($wpuserdata, $userinfo) {
  if ($wpuserdata['first_name'] == 'Dan') {
     return new WP_Error("NODAN", "We do not allow people called Dan on this site.");
  }
  return $wpuserdata;
}

add_filter('gal_pre_create_new_user', 'my_gal_pre_create_new_user', 10, 2);

Action: gal_post_create_new_user – Enterprise/Premium plugin 2.9.6+ only

The Login plugin calls this hook to inform your extension after it automatically created user when they first attempted to Login with Google.

Params:

$wpuserdata – The userdata that was passed to wp_insert_user.
$userinfo – An array containing details from the user’s Google profile (e.g. name, given_name, family_name)
$user_id – The user’s ID in WordPress

Example:

function my_gal_post_create_new_user($wpuserdata, $userinfo, $user_id) {
  // Record that the user was auto-created by the plugin
  add_user_meta( $user_id, 'is_a_google_user', true );
}

add_filter('gal_post_create_new_user', 'my_gal_post_create_new_user', 10, 3);

Filter: gal_user_new_role – Enterprise plugin only

The Login plugin applies this filter to allow your extension to change the desired role for a user when Google Groups (or email addresses) are causing the plugin to change the role.

Params:

$want_role – the role that the plugin wants to set
$user – WP_User object of the user whose role is to be set
$blogid – The blog ID (only really relevant on multisite installations)
$is_user_member – boolean specifying whether user is already a member of this blog (only really relevant for multisite – on single site this will always be true)
$in_groups – dictionary with keys showing email addresses of Google Groups of which the user is a member (including the faux Group consisting of the user’s own email address); each key maps simply to the boolean value true

For example, if the user has email address [email protected], $in_groups may equal: Array( ‘[email protected]’ => true, ‘[email protected]’ => true, ‘[email protected]’ => true )

Return:

String – the default should be $want_role if you don’t want to affect the outcome, and want the plugin to proceed with the role change as intended.
Return empty string ” to remove all privileges from the blog for the user.

Example:

function my_user_new_role($want_role, $user, $blogid, $is_user_member, $in_groups) {
   if ($blogid == 4 && $user->ID != 1) {
     // No-one should have any privileges on sub-blog 4 except user with ID 1
     return '';
   }
   return $want_role;
}

add_filter('gal_user_new_role', 'my_user_new_role', 10, 5);

Filter: gal_user_role_changed – Enterprise plugin only

The Login plugin fires this hook to notify that the user role has been changed (for a particular blog). Contrast this with the previous filter gal_user_new_role that allows you to alter the desired role before the role is set. Otherwise, the parameters are basically the same as that filter.

Params:

$want_role – the role that the plugin has set, empty string to mean no access.
$user – WP_User object of the user whose role was set
$blogid – The blog ID (only really relevant on multisite installations)
$is_user_member – boolean specifying whether user was already a member of this blog (only really relevant for multisite – on single site this will always be true)
$in_groups – dictionary with keys showing email addresses of Google Groups of which the user is a member (including the faux Group consisting of the user’s own email address); each key maps simply to the boolean value true (see gal_user_new_role for details).

Example:

Add secondary roles to the user based on group membership.

function dsl_my_user_role_changed($want_role, $user, $blogid, $is_user_member, $in_groups) {
   $extraroles = array('[email protected]' => 'payingcustomer',
                       '[email protected]' => 'courserep');

   $extra_roles = array_values(array_intersect_key($extraroles, $in_groups));

   if (is_multisite()) {
      switch_to_blog($blogid);
      $blog_user = get_userdata( $user->ID );
      foreach ( $extra_roles as $role ) {
         $user->add_role($role);
      }
      restore_current_blog();
   }
   else {
      foreach ( $extra_roles as $role ) { 
         $user->add_role($role);
      }
   }
}

add_action('gal_user_role_changed', 'dsl_my_user_role_changed', 10, 5);
Google Apps Login is trusted by thousands of organizations from small businesses to NASDAQ-listed companies and government organizations.
Users click their way through Login via Google (just one click after the first time)

Users click their way through Login via Google (just one click after the first time)

You can try it out by clicking below to auto-create a test account on this site! This will show you the basic Google login process.
Try out Google login
 

Logging in is only the start – Google Apps Login takes care of all your WordPress user management. Find out more here.

[user_show_avatar]

See [user_first_name] – that was easy!

Your account has been created, and you are now logged in. On your own site, you will specify your Google Apps domain – e.g. mycompany.com – and only users on that domain will be allowed to auto-create accounts.
Logging in is only the start – Google Apps Login takes care of all your WordPress user management.
Find out more

×