PriMoThemes — now s2Member® (official notice)

This is now a very OLD forum system. It's in READ-ONLY mode.
All community interaction now occurs at WP Sharks™. See: new forums @ WP Sharks™

Read s2Member level from database

s2Member Plugin. A Membership plugin for WordPress®.

Read s2Member level from database

Postby martonic » July 31st, 2010, 7:38 pm

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!
User avatar
martonic
Experienced User
Experienced User
 
Posts: 70
Joined: July 14, 2010

Re: Read s2Member level from database

Postby Jason Caldwell » August 5th, 2010, 3:26 pm

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");
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: Read s2Member level from database

Postby martonic » August 10th, 2010, 12:02 pm

Ah - thank you very much - I will check this out!
User avatar
martonic
Experienced User
Experienced User
 
Posts: 70
Joined: July 14, 2010

How to set reg field value?

Postby martonic » August 21st, 2010, 9:01 pm

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? :?:
User avatar
martonic
Experienced User
Experienced User
 
Posts: 70
Joined: July 14, 2010

Re: Read s2Member level from database

Postby Jason Caldwell » August 27th, 2010, 1:39 pm

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.
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: Read s2Member level from database

Postby martonic » February 14th, 2011, 9:27 pm

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
User avatar
martonic
Experienced User
Experienced User
 
Posts: 70
Joined: July 14, 2010


Return to s2Member Plugin

Who is online

Users browsing this forum: Bing [Bot], Exabot [Bot] and 2 guests

cron