WordPress Tutorials

Add Social Login to WordPress Without a Plugin

Social login lets visitors sign in to your WordPress site with their Google or Facebook account instead of creating yet another username and password. That means fewer forgotten password requests, faster signups, and a smoother experience for readers, customers, and members.

Many site owners want to add social login to WordPress without a plugin, and it is possible, but you should know the honest truth first. A true no plugin social login means writing custom OAuth code. There is no built in WordPress setting for Google or Facebook login, so the no plugin route is really a developer route: you register an app with Google and Facebook, add login buttons, and write PHP code that handles the callback, verifies the user, and logs them in.

How does social login actually work?

Social login uses OAuth 2.0, a standard way for one service to confirm who a user is without sharing their password. When a visitor clicks Login with Google, your site sends them to Google. Google asks them to sign in and approve your app, then sends them back with a temporary code. Your server exchanges that code for an access token, then uses the token to ask Google for the profile: name, email, and a unique provider ID. Your code then finds or creates a matching WordPress user and logs them in. Facebook login follows the same pattern. Verification happens on your server, not in the browser.

What do you need before you start?

You need a WordPress site with HTTPS enabled, because Google and Facebook both require a secure redirect URL. Add the PHP in a child theme or a small custom plugin so an update does not wipe your work. You also need a free Google Cloud account and a free Facebook developer account, plus a place for the buttons: the login page, a custom login page, or a registration form. If you already have a custom login page, our guide to customizing the WordPress login page without a plugin shows how to build it, and our guide to adding a login form to a WordPress page without a plugin shows how to place a working form on any page.

Step 1: Register a Google OAuth client

Go to the Google Cloud Console and create a new project, or open an existing one. Open APIs and Services, then Credentials. Click Create Credentials and choose OAuth client ID. If Google asks you to configure the consent screen first, do that now: choose External as the user type, add your site name, a support email, and your domain. Then create the client ID, choose Web application as the type, and add your exact callback URL under Authorized redirect URIs, for example https://yoursite.com/?social_login=google. The URL must match character for character, including https, or Google will reject the login. Copy the Client ID and Client Secret, and keep the secret in server side code only. Google documents the full flow at developers.google.com OAuth 2.0 documentation.

Step 2: Register a Facebook app

Go to Meta for Developers and create a new app. Choose the Consumer type, then add the Facebook Login product. In the Facebook Login settings, add your site domain and set the Valid OAuth Redirect URIs to your Facebook callback URL, for example https://yoursite.com/?social_login=facebook. Copy the App ID and App Secret from the app dashboard. Facebook apps start in development mode, which limits login to test users you add manually. To let the public log in, switch the app to Live mode and complete the basic details Facebook asks for, usually a privacy policy URL and a short description of how you use login data. Facebook documents the flow at developers.facebook.com Facebook Login documentation.

Step 3: Add the login buttons to your site

Your buttons are simple links that send the visitor to Google or Facebook with your Client ID, your redirect URL, the scopes you want, and a random state value. The state value is a security token that protects against cross site request forgery, and your callback code must check that it matches the value you stored before the redirect. Here is a simple way to build the Google login URL in PHP. Put this in your theme functions file or a small custom plugin, and call the function wherever you want the button to appear.

function vtg_social_login_buttons() {
    $state = wp_generate_password( 24, false );
    set_transient( 'vtg_oauth_state_' . $state, 1, 10 * MINUTE_IN_SECONDS );
    $google_url = add_query_arg(
        array(
            'client_id'     => 'YOUR_GOOGLE_CLIENT_ID',
            'redirect_uri'  => home_url( '/?social_login=google' ),
            'response_type' => 'code',
            'scope'         => 'openid email profile',
            'state'         => $state,
        ),
        'https://accounts.google.com/o/oauth2/v2/auth'
    );
    echo '<a class="vtg-social-btn vtg-google-btn" href="' . esc_url( $google_url ) . '">Continue with Google</a>';
}

Build the Facebook button the same way, with your Facebook App ID, the same kind of state value, and the email scope. Style both buttons with CSS and place them where visitors sign in or register.

Step 4: Handle the callback and create the user

The callback is the most important part. When Google or Facebook sends the visitor back, your code must check the state value, exchange the code for an access token with a server to server request, and fetch the user profile with that token. If any step fails, stop and show a friendly error. Once you have a verified email and provider ID, check user meta for the provider ID first, then check for a user with the same email. If that user exists, link the provider ID to that account instead of creating a duplicate. Only create a new user when neither match exists. Use wp_insert_user to create the account, store the provider ID with update_user_meta, then log the user in with wp_set_auth_cookie and wp_set_current_user.

function vtg_handle_google_callback() {
    if ( empty( $_GET['social_login'] ) || 'google' !== $_GET['social_login'] ) {
        return;
    }
    if ( empty( $_GET['code'] ) || empty( $_GET['state'] ) ) {
        wp_die( 'Login failed. Please try again.' );
    }
    $state = sanitize_text_field( wp_unslash( $_GET['state'] ) );
    if ( ! get_transient( 'vtg_oauth_state_' . $state ) ) {
        wp_die( 'Login failed. The security check did not pass.' );
    }
    delete_transient( 'vtg_oauth_state_' . $state );
    $response = wp_remote_post(
        'https://oauth2.googleapis.com/token',
        array(
            'body' => array(
                'code'          => sanitize_text_field( wp_unslash( $_GET['code'] ) ),
                'client_id'     => 'YOUR_GOOGLE_CLIENT_ID',
                'client_secret' => 'YOUR_GOOGLE_CLIENT_SECRET',
                'redirect_uri'  => home_url( '/?social_login=google' ),
                'grant_type'    => 'authorization_code',
            ),
        )
    );
    $data = json_decode( wp_remote_retrieve_body( $response ), true );
    if ( empty( $data['access_token'] ) ) {
        wp_die( 'Login failed. Google did not return a valid token.' );
    }
    $profile = wp_remote_get(
        'https://openidconnect.googleapis.com/v1/userinfo',
        array(
            'headers' => array(
                'Authorization' => 'Bearer ' . $data['access_token'],
            ),
        )
    );
    $user_info = json_decode( wp_remote_retrieve_body( $profile ), true );
    if ( empty( $user_info['email'] ) || empty( $user_info['sub'] ) ) {
        wp_die( 'Login failed. Google did not return an email address.' );
    }
    if ( email_exists( $user_info['email'] ) ) {
        $user_id = email_exists( $user_info['email'] );
        update_user_meta( $user_id, 'vtg_google_id', $user_info['sub'] );
    } else {
        $user_id = wp_insert_user(
            array(
                'user_login'   => sanitize_user( $user_info['email'] ),
                'user_email'   => sanitize_email( $user_info['email'] ),
                'display_name' => sanitize_text_field( $user_info['name'] ),
                'user_pass'    => wp_generate_password( 24, true ),
                'role'         => 'subscriber',
            )
        );
        update_user_meta( $user_id, 'vtg_google_id', $user_info['sub'] );
    }
    wp_set_current_user( $user_id );
    wp_set_auth_cookie( $user_id, true );
    wp_safe_redirect( home_url( '/my-account/' ) );
    exit;
}
add_action( 'init', 'vtg_handle_google_callback' );

This example uses the subscriber role for new users, the safe default for a blog or membership site. Never assign new social users the administrator or editor role automatically. Learn more in our guide to WordPress user roles explained. Repeat the same pattern for Facebook with its own token and profile URLs. Store the Facebook ID under a separate key such as vtg_facebook_id so a user can link both Google and Facebook to the same account.

What happens to existing users?

If someone registered with an email and password last year and now logs in with Google using the same email, they should land in the same account, not a second one. That is why the callback code checks email_exists before creating a new user. Only trust a verified email from the provider profile endpoint after a valid token check, never an email typed into a form. Keep normal password login working alongside social login, and protect the password form too: our guide to limiting login attempts in WordPress without a plugin is a good companion here.

Security notes you must not skip

  • Always verify the access token on your server by calling the provider profile endpoint. Never trust data that arrives only in the browser.
  • Always check the state parameter against the value you stored before the redirect. This blocks cross site request forgery attacks.
  • Keep your Client Secret and App Secret out of HTML, JavaScript, and public repositories. Store them in wp-config.php or the options table.
  • Use HTTPS everywhere, and make sure your redirect URIs use https, not http.
  • Ask only for the email and profile scopes. Extra permissions increase review time and reduce trust.
  • Log social login failures in a private log so you can spot abuse, and rate limit repeated failed callbacks from the same IP address.

OAuth callbacks need reliable server to server requests, so pick a host that handles WordPress well: see our web hosting guide.

The easier route: use a plugin instead

For most site owners, a plugin is the practical answer. The code above is real and it works, but it is also code you must maintain, because Google and Facebook change their APIs and review rules every year. Nextend Social Login is one of the best known free options, with Google and Facebook support in the free version and a setup wizard that walks you through the same app steps described above. Choose a plugin when you want reliability without maintenance or you do not want to debug OAuth errors yourself.

After setup, test with a second Google account before you invite real users. Check that a new login creates one subscriber account, that logging in again with the same Google account returns to the same WordPress user, and that the normal password login still works. If a login fails, check the redirect URI first, because a single mismatched character is the most common cause. Write down the Client ID and redirect URI you used, so a future change to either one does not silently break login.

Frequently Asked Questions

Can I add social login to WordPress without a plugin?

Yes, but it requires custom PHP code. Register OAuth apps with Google and Facebook, add login buttons, and write a callback handler that verifies the token, finds or creates a WordPress user, and logs them in with wp_set_auth_cookie. There is no built in WordPress setting for social login.

Is social login safe for my WordPress site?

Yes, when it is built correctly. Verify the token on your server, check the state parameter on every callback, keep your secrets private, use HTTPS, and link accounts only by a verified email from the provider.

What happens if a user already has an account with the same email?

Link the social provider ID to the existing account instead of creating a duplicate. Check user meta for the provider ID first, then check email_exists, and only create a new user when neither match is found.

Should I use a plugin or custom code for social login?

Use a plugin such as Nextend Social Login if you are not a developer or you want fast setup and long term maintenance handled for you. Use custom code if you need full control or a tailored user flow and you are comfortable maintaining OAuth code.

Last updated: November 2026.

Hostinger - Get 20% OFF

Habib ur Rehman

Hello! I'm the founder of VU Tech Geek. As a BSCS graduate and experienced WordPress professional, I'm passionate about technology and education. My Virtual University journey has given me valuable insights and knowledge, which I love to share with fellow students.… More »

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker