Blog

Blocking WooCommerce Social Login Registration

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. A note for the developers: do not throw a general Exception or WP_Error, as it won’t be caught; use the custom SV_WC_Plugin_Exception instead.

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 SV_WC_Plugin_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 \SV_WC_Plugin_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 SV_WC_Plugin_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.

One Comment

  • Bradley Griffin 2 months ago Reply

    #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?

Submit a Comment