It looks like social login plugins (such as WordPress Social Login, OneAll etc.) do not create users in the original wp-database as regular registration from Admin dashboard. Hence it is not possible to add or get user-meta-data from current user the ‘normal way’.
Question is: How to get user-meta from Social Login registered users?
I have a current case, and have not yet found he answer. Hope you can help me out.
The case
In this case we have a Gravityform which allows registered users to submit a new page. We want them to submit only one page per user. Page title is auto created and do have the form entry-ID as unique part of page title (Gravityforms settings). This function below is used to make Gravityforms to create a page instead of a post:
add_filter("gform_post_data", "change_post_type", 10, 2);
function change_post_type($post_data, $form){
//only change post type on form id 1
if($form["id"] != 2)
return $post_data;
$post_data["post_type"] = "page";
return $post_data;
}
Now we want users who have completed the form successfully and are currently logged in, to show the url of their created page like www.example.com/pagenumber{entry-ID}
Therefore there are 3 options what to display:
- user is logged in and have created a page -> show page url
- user is logged in and have not yet created a page -> show form -> redirect to point 1 after completing
- user is not logged in -> show social login buttons -> redirect to point 2 after connecting
First we need to add the form entry ID to the user-meta:
add_action( 'gform_after_submission', 'wpse96480_map_user_to_page', 10, 2);
function wpse96480_map_user_page( $entry, $form ) {
$user_id = $entry['created_by'];
$meta_key = 'generated_page_id';
$meta_value = $entry['post_id'];
$unique = true;
add_user_meta( $user_id, $meta_key, $meta_value, $unique );
}
Now we add the code below to the page.php (or page template file) to check if current user is logged in and have an entry-ID in user-meta, and if so, to display the url with the entry-ID:
<?php
if ( is_user_logged_in() ) {
global $current_user;
// $current_user = get_currentuserinfo();
$user_id = $current_user->ID;
$meta_key = 'gform_entry_id';
$single = true;
$entry_id = get_user_meta( $user_id, $meta_key, $single );
if( strlen( $entry_id ) > 0 && is_numeric( $entry_id ) ) {
// we have an entry ID now
?>
<h2>Hey <?php echo $current_user->display_name ?>, thank you for submitting the form. Visit your page here: www.example.com/pagenumber<?php echo( $entry_id ); ?></h2>
<?php
} else {
// we don't have an entry ID for this user
?>
<h2>Hey <?php echo $current_user->display_name ?>, Thank you for joining. To create a page please submit the form below:</h2><?php echo do_shortcode('[gravityform id="2" name="just a form" title="false" description="false"]'); ?>
<?php
}
} else {
// user is not logged in
?>
<h2><Please log in to create a page <?php do_action( 'wordpress_social_login' ); ?></h2>
<?php
}
?>
The result:
If a regular registered WP user (who is created by Admin in the Admin dashboard) is logged in, and completes the form successful, option (1) is effective/successful showing as we wanted. If user have not completed the form yet, option (2) is effective/successful showing.
The problem with Social Login users:
If a user registered via a Social Login Plugin is logged in, and have completed the form successful, option (2) is still showing like user has never completed the form.
Question:
How to get user-meta from Social Login registered users?**
You do not specify a plugin you are using. But if a user login with a OAuth (used by the mosts social plattforms), then he cannot be logged in in WordPress.
Why?
A user needs a username and password to login. OAuth (and other methods) do not provide a password. They only answers with Yes, this user is authorized or No, not authorized.
The social login plugins need a method to re-identify the user if he request a new page from the blog. They have to store something that tells them This user is logged in. WordPress use a cookie. Another method is to start a session. A bit exotic method could use the database and the ip adress. I don’t know how your social login plugin re-identify the user,but this is a important point to find out why your formular doesn’t work.
Why is the user not longer logged in?
I guess the plugin lose the connection to the re-identify method. Means, cannot read the cookie or the session is stopped/destroyed.
Possible solution
When displaying the formular and if the user is logged in with a social login, register a new user in WordPress and log this user in. This could be a dummy user and do not need a real username. Check if the current user is logged in with a social log in, fetch the userdata (e.g. username),create a hash from this userdata, use this hash as username and password. The next time the user logged in with the social login, hook into this process, grab the userdata, create a hash and check if a user with this hash exists.
You can also try to find out how the social login re-identify the user and why he isn’t re-identified after the formular is finished.