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 1
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']);
}
});
Example 2
Using trackerdata to override the role that the user gets registered with:
Suppose you have a marketplace, where you allow the registration with 2 different roles: vendor and customer. For each role you have different registration forms.
Normally, the registration with social login won’t consider the roles of the forms, as by default Nextend Social Login registers the users with the WordPress default role set at WordPress > Settings > General > New user default role. To make the social login register new accounts with:
- the customer role in the customer registration form
- the vendor role in the vendor registration form
You need the following setup:
- To display the social buttons in the customer form, publish them using the shortcode, e.g.:
[nextend_social_login]
- Set the WordPress default role to “customer”, this way the registration with social login will automatically assign the customer role to the new account
- To display the social buttons in the vendor form and to be able to identify these social buttons for the vendor registration, publish them using the shortcode with the trackerdata parameter, e.g.:
[nextend_social_login trackerdata="vendor"]
- Finally, to register the users with the vendor role in the vendor form, use the
nsl_register_roles
filter to override the default role, whenever the tracker data matches the one you set for the vendor specific shortcode. E.g.: