Page 1 of 1

Read s2Member level from database

PostPosted: July 31st, 2010, 7:38 pm
by martonic
Hi again Jason,

Here is my code to get the s2Member level out of the database.

Code: Select all
   $key = "wp_capabilities";
   $fields = get_user_meta($wp_id, $key, false);
   $level = 0;
   if ($fields != null && is_array($fields)) {
        for ($i = 0;  $level == 0 && $i < sizeof($fields); $i++) {
            if      (array_key_exists("s2member_level4", $fields[$i])) $level = 4;
            else if (array_key_exists("s2member_level3", $fields[$i])) $level = 3;
            else if (array_key_exists("s2member_level2", $fields[$i])) $level = 2;
            else if (array_key_exists("s2member_level1", $fields[$i])) $level = 1;
        }
    }


Is there an easier way to do it?

Also, how do you set it? I need to set it, I am overwriting the wp_capabilites record (since add_usermeta does not work with your strings). Oy! Can you help with this? Thanks!

Re: Read s2Member level from database

PostPosted: August 5th, 2010, 3:26 pm
by Jason Caldwell
Hi Martonic. Thanks for the great question.
~ Sorry for my delayed response.

Here is how I would determine the role of a User:
( this is just some pseudo code to clarify how it's done )
Code: Select all
$user = new WP_User([ existing user ID or a Username is also fine ]);
$role = $user->roles[0]; // subscriber, or s2member_level[1 2,3,4]

Here is an actual example:
Code: Select all
$user = new WP_User(123);
echo $user->roles[0]; // s2member_level1

Now, assigning a new Role is very easy.
Code: Select all
$user = new WP_User(123);
$user->set_role("s2member_level2");

Re: Read s2Member level from database

PostPosted: August 10th, 2010, 12:02 pm
by martonic
Ah - thank you very much - I will check this out!

How to set reg field value?

PostPosted: August 21st, 2010, 9:01 pm
by martonic
Hi again Jason,

Thank you very much for the ongoing fixes and improvements. :mrgreen:

What is the code to set a "custom registration field"? That is, if there is a custom registration field called "telephone", what is the php code to set its value? :?:

Re: Read s2Member level from database

PostPosted: August 27th, 2010, 1:39 pm
by Jason Caldwell
Hi there. You're very welcome.
~ Thanks for your patience.

That's a great suggestion for an API function. @TODO.
Until there is an API function integrated into s2Member for this, here's how it works:
Code: Select all
global $current_user;
$custom_fields = get_user_option("s2member_custom_fields");

$custom_fields["telephone"] = "999-999-9999";
update_user_option($current_user->ID, "s2member_custom_fields", $custom_fields);

Of course, you could also do this manually through the Dashboard. Go to "Users" in your Dashboard, and just click the "Edit" link, there you can change all of the Custom Fields manually.

Re: Read s2Member level from database

PostPosted: February 14th, 2011, 9:27 pm
by martonic
Hi Jason,

It's been a while - hope you're doing great! :mrgreen:

Congratulations on a fine piece of coding to make a nice interface for configuring arbitrary user-defined select lists and so on - very good stuff.

If a Custom Registration Field has a drop-down list, and we enter data pairs (name|value) for the drop-down list, where in the database do these data pairs get stored?

I found it - JSON encoded data in wp_options.

I need to write a function to swap out the entire list and replace it with a new set of data pairs (for a given Custom Registration Field).

I will write a test procedure to grab the data, decode and dump it - and take it from there. Any pointers on constructing this little function will be greatly appreciated.

Thanks a million, Marty