3

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:

  1. user is logged in and have created a page -> show page url
  2. user is logged in and have not yet created a page -> show form -> redirect to point 1 after completing
  3. 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?**

2
  • You can find the source of the above code here: wordpress.stackexchange.com/q/96468/31765
    – tobe
    Apr 23, 2013 at 11:27
  • I checked the Database, and it looks like there are no additional user meta added to users who have registered via social login. Normal users have about 20 meta's, but social users almost none (2-4). In the database I also noticed a table with title wp_social_users. Any idea what that is?
    – tobe
    Apr 24, 2013 at 7:38

1 Answer 1

2

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.

3
  • Thank you. I have compared the WP and Social Login users in the DataBase. I found out that the WP users have lots of user-meta. The Social Login users have only 2-4 (depending on which plugin is used for Social Login). So the problem was not identifying the user, or get the user display name (from Social Login users) But it looks impossible to create a user-meta for users registered via Social Login Plugins. It has something to do with Wordpress itself. So I guess there is still something to discover ;-) I found another solution for my Question: wordpress.stackexchange.com/a/97079/31765
    – tobe
    Apr 24, 2013 at 14:33
  • PS: I tested with several Social Login Plugin's, they have all the same problem with creating user-meta for users registered with Social Login.
    – tobe
    Apr 24, 2013 at 14:33
  • @tobe -- I'm disappointed that you couldn't find a way or plugin to use both Social Logins and WP logins with enough user-meta. I'll post a decent question to see if one yet exists when I know how. I'd like to never store any user passwords or email addresses in WP, and yet be able to create robust user accounts or send them email.
    – DaaBoss
    Feb 5, 2015 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.