Backend Developer Docs

Actions

nsl-add-providers

This action helps you adding new providers to the plugin.

nsl-providers-loaded

This action runs after every provider has been loaded and ordered correctly.
NextendSocialLogin::$providers contains all available providers.
NextendSocialLogin::$enabledProviders contains only the enabled providers.

Connect Flow actions/filters

{{provider-id}}_login_action_before

This action run first after the user clicked the social login button. It has only one parameter, the current provider. It extends from the NextendSocialProvider class.

{{provider-id}}_login_redirect_url

It’s actually a filter, not an action. It has two parameters:

  • current redirect URL
  • current provider

It returns the new redirect URL.
Example usage:
add_filter(facebook_login_redirect_url, function($redirectUrl, $provider){ return $redirectUrl;}, 10, 2);

{{provider-id}}_register_redirect_url

It’s the same as the {{provider-id}}_login_redirect_url. It has the same two parameters, but it happens upon registration:

  • current redirect URL
  • current provider

nsl_register_new_user

This happens when a new user is registered with any of the providers. Parameteres:

  • $user_id
  • provider instance

nsl_{{provider-id}}_register_new_user

This action runs when the user registers with the given provider. Parameters:

  • $user_id
  • provider instance

nsl_{{provider-id}}_auto_link_allowed

This action prevents linking the user accounts automatically for a user when it returns false.
Parameters:

  • $isAutoLinkAllowed
  • current provider instance
  • $user_id

nsl_{{provider-id}}_is_login_allowed

This action prevents the login for the user when it returns false.
Parameters:

  • $isAutoLoginAllowed
  • current provider instance
  • $user_id

nsl_login

This action runs when a user logs in with any of the providers.
Parameters:

  • $user_id
  • provider instance

nsl_{{provider-id}}_login

This action runs when a user logs in with the given provider.
Parameters:

  • $user_id
  • provider instance
  • access_token

nsl_pre_register_new_user

This action runs before the new user is registered.
Parameters:

  • NextendSocialUser class instance

nsl_is_register_allowed

Checks whether the user registration is allowed.
Parameters:

  • boolean
  • provider instance

Available since: 3.0.4

nsl_{{provider-id}}_link_user

This action runs when a user link the account to a given provider, registration with the given provider also triggers this hook.
Parameters:

  • $user_id
  • provider id

nsl_registration_user_data

This filter can be used for overriding certain userdata before the registration or preventing the registration.
Parameters:

  • $userData – associative array – username and email
  • $provider – provider instance, that can be used to retrieve some data of current user.
  • $error – error object, that can be used for preventing the registration.

Using the trackerdata shortcode function

Nextend Social Login allows you to do custom actions when the user logs in using the login buttons. Add the trackerdata attribute to the shortcode:

[nextend_social_login trackerdata="my-tracker-data"]

Then you can write custom codes to change the user role based on the page where the user has registered or to save down extra information.

Example: using trackerdata to identify the page the user registered

For this you’ll need to use the shortcode with a custom trackerdata value. In this tutorial the registered_from value will be used

[nextend_social_login trackerdata="registered_from"]

Then you’ll need to set up cookies to save down the URL of the current page:

<script type="text/javascript">
    function setCookie(name,value,days) {
  var expires = "";
  if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days*24*60*60*1000));
    expires = "; expires=" + date.toUTCString();
  }
  document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
setCookie('registered_from', '<?php global $wp; echo home_url( $wp->request ) ?>', 7);</script>

And you’ll also need a PHP code to work with the cookie. The nsl_register_new_user action runs just before the registration process is finished, so you can use it to get the tracker data and add a new user meta.

<?php add_action('nsl_register_new_user', function ($user_id) {
    if (NextendSocialLogin::getTrackerData() == "registered_from") {
        $user = new WP_User($user_id);
        add_user_meta($user->ID, 'registered_from', $_COOKIE['registered_from']);
    }
});?>

You could create a custom plugin to add these codes. You can find a working sample below:

<?php
/*
Plugin Name: NSL trackerdata
Description: Track the page where users logged in using Nextend Social Login
Version: 1
 */

add_action('wp_head', 'set_custom_cookie');
function set_custom_cookie(){
    ?>
    <script type="text/javascript">
    function setCookie(name,value,days) {
  var expires = "";
  if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days*24*60*60*1000));
    expires = "; expires=" + date.toUTCString();
  }
  document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
setCookie('registered_from', '<?php global $wp; echo home_url( $wp->request ) ?>', 7);</script>
    <?php
};

add_action('nsl_register_new_user', function ($user_id) {
    if (NextendSocialLogin::getTrackerData() == "registered_from") {
        $user = new WP_User($user_id);
        add_user_meta($user->ID, 'registered_from', $_COOKIE['registered_from']);
    }
});

Validate Username

You can add custom validation to the entered username using the nsl_validate_username filter.

Example:

add_filter('nsl_validate_username', function ($isValid, $username, $errors) {
    if (strlen($username) < 6) {
        $errors->add('invalid_username', '' . __('ERROR') . ':' . __('Sorry, username must contain at least 6 characters.'));
        $isValid = false;
    }
    return $isValid;
}, 10, 3);

Available since: 3.0.7

Override the userdata (username/email) before registration

You can override the username or the email in the $userData using the nsl_registration_user_data filter.

Example:

add_filter('nsl_registration_user_data', function($userData, $provider){
    $userData['username']=$provider->getAuthUserData('first_name').'-'.$provider->getAuthUserData('last_name');
            
    return $userData;
},10,2);

Available since: 3.0.15

Prevent registration with certain email domain

You can either allow or prevent registration with certain email domains using the nsl_registration_user_data filter.

Example – disable registration with the gmail.com email domain:

add_filter('nsl_registration_user_data', function ($user_data, $provider, $errors) {
    /**
     * List of banned domains. Multiple domains can be separated with commas e.g.: "gmail.com","yahoo.com"
     */
    $banned_domains = array("gmail.com");
    if (isset($user_data['email']) && !empty($user_data['email']) && is_email($user_data['email'])) {
        $email_parts = explode('@', $user_data['email']);
        if (in_array($email_parts[1], $banned_domains )) {
            /**
            * The error message.
            */
            $errors->add('invalid_email', '' . __('ERROR') . ': ' . __('Sorry, registration with this email domain is not allowed!'));
            return $user_data;
        }
    } else {
        $errors->add('invalid_email', '' . __('ERROR') . ': ' . __('Sorry, email is missing or invalid!'));
    }

    return $user_data;
}, 10, 3);

Available since: 3.0.20

Asking custom fields before the registration

You can use Nextend Social Login to ask custom information form the users before the registration process is finished and you can also validate this data.

In the example below you can see how to ask the favorite color of the person who registers.