Backend Developer Docs

Warning:

You need to have a developer’s knowledge to be able to use the actions and filters below. We are not able to provide support for custom coding!

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

A filter to override the login redirect url. It has two parameters:

  • Fixed redirect URL, if set
  • 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:

  • Fixed redirect URL, if set
  • 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

This action runs when a user unlinks a social media account from a certain provider.
Parameters:

  • $user_id
  • provider id

Available since: 3.0.28

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.

nsl_disabled_register_redirect_url

This filter can be used for overriding the URL where the user will be redirected, if the registration is disabled.
Parameters:

  • $redirectUrl – string – the url of the page

Available since: 3.0.25

nsl_disabled_register_error_message

This filter can be used for overriding the message that is displayed when the registration is disabled with Nextend Social Login.
Parameters:

  • $errorMessage – string – the error message

Starting from version 3.0.28 passing “false” in the return value can be used for disabling the default error message!

Available since: 3.0.25

nsl_{{provider-id}}_auth_url_args

This filter can be used for overriding authorization URL arguments of each provider.
Parameters:

  • $args – associative array – contains the current authorization URL arguments.

Available since: 3.0.26

This filter can be used for overriding the User ID that the social account should be linked to.
Parameters:

  • $user_id – the ID of the WordPress account where the social account should be linked to
  • social user instance
  • current provider instance

Available since: 3.0.26

nsl_disabled_login_redirect_url

This filter can be used for overriding the URL where the user will be redirected, if the login is disabled.
Return value:

  • string – the URL where the user should be redirected

Available since: 3.0.27

nsl_disabled_login_error_message

This filter can be used for overriding the message that is displayed when the login is disabled with Nextend Social Login.
Return value:

  • string – the error message

Available since: 3.0.27

nsl_already_linked_error_message

This filter can be used for overriding the message that is displayed when a person tries to connect with a social media account, that’s email address is registered on the site and has another social media account from the same kind already linked.
Return value:

  • string – the error message

Available since: 3.0.30

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.

Redirect user on login depending on its role

By combining the ‘nsl_login’ action and the ‘{{provider-id}}_login_redirect_url’ filter, you can control where the user with a specific role should land.

In this example, you can see a basic code snippet which redirects the users with the “subscriber” role to a custom link.

Exclude URL from Fixed redirect url – for Login/Register setting

In this example, you can see a basic code snippet which will prevent the Fixed redirect url – for Login/Register setting from redirecting to the specified URL, if the Google button is pressed on the ‘https://example.com/checkout’ page: