Ask SkyVerge

We’ve got an Ask SkyVerge question today from Jonathan that’s a neat usage of Social Login hooks:

Is there a way to only allow social login for people who have an account already? Meaning we don’t want to create an account for them automatically since they should purchase a memberships to log in.

Sure thing — there are hooks that will let us hook into Social Login just before it creates an account to stop it from doing so. However, there is one thing you should note here: do not show Social Login on checkout pages then (or at least change your messaging), as people won’t be able to register at all using Social Login; it will only be available for existing accounts.

With that said, let’s take a look at the code and what it does!

Blocking User Registration

Social Login will fire an action right before creating a new user if it doesn’t find an existing one: wc_social_login_before_create_user

This hook will let us completely stop the registration process if desired by throwing an Exception, which is a type of error that will halt the rest of the plugin’s processing of a user login.

So let’s have a look! I’ll start out with my site’s social login form using Disqus:

WooCommerce Social Login Account page

We’ll need to only add a small snippet to throw the error here, stopping Social Login from logging the user in, and returning an error message instead (you can tweak the messaging as desired).

// Don't allow new user registrations via Social Login.
function sv_wc_social_login_prevent_registration() {
    throw new Exception( __( 'Hmm, this profile is not linked to an account.', 'my-textdomain' ) );
add_action( 'wc_social_login_before_create_user', 'sv_wc_social_login_prevent_registration' );

That’s it! Now if I log in with an account already linked to Disqus, or one that has an email that matches my Disqus account, I’m still logged in as expected. However, if I don’t yet have an account with the site, the error is shown instead of creating a new account:

WooCommerce Social Login: User Registration blocked 1

Taking it Further: Include Provider Name

Now of course, this is a simple example 🙂 We can certainly take things further, as this hook gives you access to the social account profile that the network returns, as well as the provider ID so we can get provider information (like the social network name). Any time you have a provider ID in WooCommerce Social Login, you can get that provider class using: wc_social_login()->get_provider( $provider_id )

So let’s throw a more descriptive error here in case the site uses multiple providers: include the provider name as ask the user to link the profile instead.

 * Don't allow new user registrations via Social Login; ask users to link the account instead.
 * @param \WC_Social_Login_Provider_profile $profile profile instance
 * @param string $provider_id Social Login provider ID
 * @throws \Exception
function sv_wc_social_login_prevent_registration( $profile, $provider_id ) {

    // we don't need to check that we've got a valid provider, an exception would have already been thrown if not
    throw new Exception( sprintf(
        __( 'Hmm, this %s profile is not linked to an account. Please log in with your password to link it.', 'my-textdomain' ),
        wc_social_login()->get_provider( $provider_id )->get_title()
    ) );
add_action( 'wc_social_login_before_create_user', 'sv_wc_social_login_prevent_registration', 10, 2 );

And voilà, our error message will be more descriptive, using the provider name in the message:

WooCommerce Social Login: User Registration blocked 2

You could take this further with the customer profile as well, such as pulling name or email address details from the returned profile to personalize this message.

Want to have a look at the full code? Check out the full snippet here.

Published by Beka Rice

Beka leads product direction for SkyVerge and technical documentation. She spends a lot of time on research and interviews, but likes to write so she has an excuse to spend more time jamming out to anything from The Clash to Lady Gaga.


  1. #roadtrip #3kids #LongPottyBreak #multitaskingFTW!

    Idea: Remove as much/as many WP registration aspects as possible and ONLY present social login as an option for users to register

    Ever been done? Considered?

  2. Unfortunately, the above snipped causes a 500 error on the latest versions of WordPress and Woocommerce. Tested with multiple themes included WooCommerces latest.

    • Hey Antony! We’re going to update the plugin to catch a general Exception in the next update, as we’ve recently changed how our plugin framework handles this situation. I’ll update this post once the new release is out. 🙂

Hmm, looks like this article is quite old! Its content may be outdated, so comments are now closed.