Having trouble storing the discord id after a user signs in with discord/wordpress

I can't seem to get the discord id of a user when they sign in. How would I do it?

Best Answer

  • Claude_SchlesserClaude_SchlesserAdministratorOneAll Team
    edited July 30 Answer ✓

    Here a more complete action to save the Discord username, userid and discriminator:

    //Handle data retrieved from a social network profile
    function oa_social_login_store_extended_data ($user_data, $identity)
    {
        if ($identity->source->key == 'discord')
        {
            $account = array_shift($identity->accounts);
    
            if (isset ($account->username)
            {
                $parts = explode ('#', $account->username);
                if (count ($parts) == 2)
                {
                    update_user_meta ($user_data->ID, 'discord_username', $parts[0]);
                    update_user_meta ($user_data->ID, 'discord_discriminator', $parts[1]);
                }
                else
                {
                    update_user_meta ($user_data->ID, 'discord_username', $parts[0]);   
                }
            }
    
            if (isset ($account->userid)
            {
                update_user_meta ($user_data->ID, 'discord_userid, $account->userid);
            }
        }
    }
    
    //This action is called whenever Social Login adds a new user
    add_action ('oa_social_login_action_after_user_insert', 'oa_social_login_store_extended_data', 10, 2);
    

Answers

  • Claude_SchlesserClaude_SchlesserAdministratorOneAll Team
    edited July 30

    Hello,

    you can use the following hook:
    http://docs.oneall.com/plugins/guide/social-login-wordpress/#3a

    And then a function like this:

    function oa_social_login_store_extended_data ($user_data, $identity)
    {
        if ($identity->source->key == 'discord')
        {
            if (preg_match ('#/([0-9]+)/#', $identity->id, $matches))
            {
                update_user_meta ($user_data->ID, 'discord_userid', $matches[1]);  
            }
        }
    }
    
    //This action is called whenever Social Login adds a new user
    add_action ('oa_social_login_action_after_user_insert', 'oa_social_login_store_extended_data', 10, 2);
    

Welcome!

Please sign in to your OneAll account to ask a new question or to contribute to the discussions.

Please click on the link below to connect to the forum with your OneAll account.