WordPress plugin to login/register with Google
Login with Google provides seamless experience for users to login in to WordPress sites using their Google account. No need to manually create accounts, no need to remember quirky passwords. Just one click and land into the site!
- Clone this repository.
- Run
composer install --no-devinside the cloned directory. - Use
nvmto install the recommended node version. - Use
npm ito install the dev dependencies. - Run
npm run productioninside the cloned directory. - Upload the directory to the
wp-content/pluginsdirectory. - Activate the plugin from the WordPress dashboard.
These browsers are supported. Note, for example, that One Tap Login is not supported on Edge in iOS.
-
You will need to register a new application at https://console.cloud.google.com/apis/dashboard
-
Authorization callback URLshould be likehttps://yourdomain.com/wp-login.phpand theAuthorized JavaScript originsshould be likehttps://yourdomain.comwherehttps://yourdomain.comwill be replaced by your site URL. -
Once you create the app, you will receive the
Client IDandClient Secret, add these credentials inSettings > Login with Googlesettings page in their respective fields. -
Create new userenables new user registration irrespective ofMembershipsettings inSettings > General; as sometimes enabling user registration can lead to lots of spam users. Plugin will take this setting as first priority and membership setting as second priority, so if any one of them is enabled, new users will be registered by this plugin after successful authorization. -
Whitelisted Domainsallows users from specific domains (domain in email) to get registered and log in to the site. This prevents unwanted registration and unauthorized domain logins. For Example: If you want users only from your organization (myorg.com) to access the website, entermyorg.comin whitelisted domains. Users with Google email likeabc@myorg.comwill be able to register and log in. Contrary to this, users with emails likesomething@gmail.comwill not be able to log in or register. To disable domain enforcement on login for existing users, use thertcamp.google_enforce_whitelisted_domains_on_loginfilter. -
Google Account Linking & Resetting Links: When a user logs in with Google for the first time, their unique Google account ID (sub) is permanently bound to their WordPress user account (google_oauth_subuser meta) to protect against account takeover. If an administrator needs to unlink or reset a user's Google account link (for example, if a user changes their Google account), they can delete the user meta via WP-CLI:
wp user meta delete <user_id> google_oauth_subOr in PHP:
delete_user_meta( $user_id, 'google_oauth_sub' );One Tap Authentication & Two-Factor Authentication: During One Tap authentication, the standard WordPressauthenticateandwp_authenticate_userfilters are invoked before setting authentication cookies, allowing user lockout and approval plugins to hook in. (Note: These filters run after user resolution or creation, so if an authentication filter returns aWP_Error, cookies are not set andwp_logindoes not fire, but any newly created user record remains in the database). If the Two-Factor plugin is active and configured for the user, One Tap redirects the user towp-login.php?login_with_google_2fa=1with their intendedredirect_totarget so they can complete secondary verification securely.
Above mentioned settings can also be configured via PHP constants by defining them in wp-config.php file.
Refer following list of constants.
| Type | Description | |
|---|---|---|
| WP_GOOGLE_LOGIN_CLIENT_ID | String | Google client ID of your application. |
| WP_GOOGLE_LOGIN_SECRET | String | Secret key of your application |
| WP_GOOGLE_LOGIN_USER_REGISTRATION | Boolean | (Optional) Set True If you want to enable new user registration. By default, user registration defers to Settings > General Settings > Membership if constant is not set. |
| WP_GOOGLE_LOGIN_WHITELIST_DOMAINS | String | (Optional) Domain name, if you want to restrict login with your custom domain. By default, It will allow all domains. You can whitelist multiple domains. |
These constants can also be configured via wp-cli.
Note: If you have defined the constant in wp-config.php file, corresponding settings field will be disable (locked for editing) on the settings page.
| Filter | Description | Parameters |
|---|---|---|
rtcamp.google_scope |
This filter can be used to filter existing scope used in Google Sign in. You can ask for additional permission while user logs in. |
|
rtcamp.google_login_modules |
Filter out active modules before modules are initialized. |
|
rtcamp.google_login_button_display |
This filter is useful where we want to forcefully display login button, even when user is already logged-in in system. |
|
rtcamp.google_default_redirect |
Filter the default redirect URL in case redirect_to param is not available. Default to admin URL. |
|
rtcamp.google_register_user |
Check if we need to register the user. |
|
rtcamp.google_client_args |
Filter the arguments for sending in query. This is useful in cases for example: choosing the correct prompt. |
|
rtcamp.google_login_state |
Filters the state to pass to the Google API. |
|
rtcamp.default_algorithm |
Filters default algorithm for openssl signature verification |
|
rtcamp.google_redirect_url |
Filters the URL to which the user will be redirected post successful authentication |
|
rtcamp.google_enforce_whitelisted_domains_on_login |
Filter whether to enforce whitelisted domains on login for existing users. Defaults to true. |
|
| Action | Description | Parameters |
|---|---|---|
rtcamp.google_login_services |
Define any additional services. |
|
rtcamp.google_user_authenticated |
Fires once the user has been authenticated via Google OAuth. |
|
rtcamp.id_token_verified |
Do something when token has been verified successfully. If we are here that means ID token has been verified. |
|
rtcamp.google_user_logged_in |
Fires once the user has been authenticated. |
|
rtcamp.google_user_created |
Fires once the user has been registered successfully. |
|
rtcamp.login_with_google_exception |
Fires when an exception is raised during token verification. |
|
You can add the Google login button to any page/post using shortcode: google_login
Example:
[google_login button_text="Google Login" force_display="yes" /]Supported attributes for shortcode
| Parameter | Description | Values | Default |
|---|---|---|---|
| button_text | Text to show for login button | string | Login with Google |
| force_display | Whether to display button when user is already logged in | yes/no | no |
| redirect_to | URL where user should be redirected post login | URL | wp-admin |
- For contributing to this plugin, please refer to CONTRIBUTING.md for more details.
Unit tests can be run with simple command composer tests:unit.
Please note that you'll need to do composer install (need to install dev dependencies) for running
unit tests.
You should have PHP CLI > 7.1 installed. If you have Xdebug enabled with php, code coverage report will be
generated at /tmp/report/html
Code snippets to extend and customize the plugin can be found here.
WordPress >= 5.5.0
PHP >= 7.4
This library is released under "GPL 2.0 or later" License.
