latest stable versions: v150827 (changelog)

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

create unique user ID – s2member custom field

Home Forums Community Forum create unique user ID – s2member custom field

This topic contains 4 replies, has 3 voices. Last updated by  Raam Dev 4 years, 4 months ago.

Topic Author Topic
Posted: Wednesday Sep 5th, 2012 at 7:34 am #24148
Robert H.
Username: robaggio

Hello,
I’d like to add a custom user field to dynamically create a unique 32 character user ID when a user registers with s2member.
I created a custom profile field ID called “apikey”. Then I added this code to s2-hacks.php.

Unfortunately, no unique ID is being generated when registering a new user.

Maybe, the code example provided from Jason is not working for my purpose.

The PHP code works without drouble – see http://lernpress.de/apikey.php

<?php
echo md5(uniqid(mt_rand(), true));
?>

Would be great if you guys can help me out.
Thanks

http://www.primothemes.com/forums/viewtopic.php?f=4&t=2822
http://www.s2member.com/codex/stable/source/s2member/includes/classes/custom-reg-fields.inc.php/
http://blog.vivekv.com/uniqid-the-easiest-way-to-generate-unique-strings.html

<?php
/* ### Setting options dynamically for Custom Registration Fields. ### */
add_action ("ws_plugin__s2member_before_custom_field_gen", "apikey_field_options");
function apikey_field_options ($vars = array ())
    {
        $_field = &$vars["__refs"]["_field"]; /* By "reference". */
        /* See: http://www.php.net/manual/en/language.references.spot.php. */
        /**/
        if ($_field["id"] === "apikey")
            {
                $_field["options"] = md5(uniqid(mt_rand(), true));
            }
    }

/*
Setting a default value for Custom Registration Fields.
*/
add_action ("ws_plugin__s2member_before_custom_field_gen", "apikey_field_values");
function apikey_field_values ($vars = array ())
    {
        $references = &$vars["__refs"]; /* Array of variable "references". */
        $_field = &$vars["__refs"]["_field"]; /* By "reference". */
        /* See: http://www.php.net/manual/en/language.references.spot.php. */
        /**/
        if ($_field["id"] === "apikey")
            {
                if (empty ($references["_submission"]) && empty ($references["_value"]))
                    /* If a form was not just submitted. And, there is no default value. */
                    
                    $references["_value"] = md5(uniqid(mt_rand(), true)); /* Set the default value. */
            }
    }

?>

List Of Topic Replies

Viewing 4 replies - 1 through 4 (of 4 total)
Author Replies
Author Replies
Posted: Thursday Sep 6th, 2012 at 4:35 pm #24383
Robert H.
Username: robaggio

Anybody who can help?
Thanks

Posted: Friday Sep 7th, 2012 at 5:52 am #24493

Hi Robert.

The hack you quoted is for s2Member custom registration fields, but the username is not one, it’s part of WP itself.

I guess you can have some JavaScript that’ll populate the username in a hidden field of the registartion form. Here’s one that creates a username off of the email address: http://www.primothemes.com/forums/viewtopic.php?f=4&t=15672&p=49082#p49082

Or you can hack the WP registration code so it does what you want. I haven’t played with this at all to give a code sample to try.

Have you searched if there is a plugin that generates usernames automatically? https://wordpress.org/extend/plugins/

I hope it helps. :)

Posted: Friday Sep 7th, 2012 at 9:22 am #24516
Robert H.
Username: robaggio

Thanks Cristián,

I am not talking about the regular WP user ID.
I’d like to generate a unique API-key for each subscriber during the registration process.
This API-key should then be saved in a user meta field.
This process should be hidden for the subscriber and the value is not editable.
A hidden custom registration field for this purpose seems to be a good solution for me.

After registration and payment the subscriber needs to enter this API-key into a plugin in order to get access to content delivered by the plugin. S2member checks if subscriber is still a paying member.

This code generates a unique 32 character string. So I think it’s perfect to use it as API-key.
Actually, this would be great to have it as a built-in feature in S2member Pro. Specially for services like WP subscription plugins as business models.

<?php
echo md5(uniqid(mt_rand(), true));
?>

e.g. 280bfcaa917c9a232293fa7475528658

I checked all the WP user meta plugins but none of them can prepopulate user meta field values dynamically generated from PHP code.

Hope I could clarify my problem.
Would be great if you have a solution

Thanks

Posted: Saturday Sep 8th, 2012 at 8:24 am #24631
Raam Dev
Username: Raam
Staff Member

Hi Robert,

There are two ways you could do this:

1. You could create a Custom Registration field that shows up during registration, then use some JavaScript + PHP to fill in the value of that field with the API key that you generate, then finally hide the field using CSS so that the user never sees that it’s there. When the form is submitted, the API key will be recorded to their account. (You’ll need to use a plugin like Exec-PHP to run PHP on the page where you display the Pro-Form.)

2. The other way to do this would be to hook into the registration process using one of the Registration Hooks and then generate your API key and add it to the Custom Registration Field. However, with the way s2Member currently stores Custom Registration Fields in the database, adding the data via PHP might be rather tricky (see this thread for more info).

I have personally used the first method and it works just fine.

Viewing 4 replies - 1 through 4 (of 4 total)

This topic is closed to new replies. Topics with no replies for 2 weeks are closed automatically.

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Contacting s2Member: Please use our Support Center for bug reports, pre-sale questions & technical assistance.