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™

Is it possible to change ccap value through API?

s2Member Plugin. A Membership plugin for WordPress®.

Is it possible to change ccap value through API?

Postby slyhands » November 2nd, 2011, 12:22 pm

Hello -

First of all...hats off to the s2member team. You guys have really created a phenomenal extension to Wordpress that is extremely well documented. It will be exciting to see what advances you continue to make as time goes on!

OK...the background:
I have successfully setup s2member pro for a client and everything is running great. They have about 50 or so members using the site...all is good. The client has recently requested that they would like to add an option for members to participate in the "Wall of Heroes", in which the members name will appear on a page if they are participating.

I added a custom registration field (under the General Options > Custom Registration Fields/Options). It is named "wall_of_heroes" with two potential values: 'yes' or 'no' (default is "yes"). This option is now successfully presented to a new person signing up for the site.

I have also created some code where I can successfully read and/or evaluate the value of wall_of_heroes utilizing:

$fields = json_decode(S2MEMBER_CURRENT_USER_FIELDS, true);
if (current_user_can('access_s2member_level0') && $fields["wall_of_heroes"] === 'yes') {
// YES FOR WALL OF HEROES
}

So far...so good!

The Problem
Okay...so everything up until now is fantastic. But my (long-winded) question is: How can I modify the "wall_of_heroes" value through code?

For starters, I have about 50 other members whose value I need to change. Previous members do not yet have a value in this field.

I have another custom field that I will need to modify depending if they complete something on the site or not (this value will not be changed by the user...but only through the code I create).


Any direction on this matter would be fantastic. Thank you in advance for any help you can give.

Steve
User avatar
slyhands
Registered User
Registered User
 
Posts: 36
Joined: September 1, 2011

Re: Is it possible to change ccap value through API?

Postby Cristián Lávaque » November 2nd, 2011, 11:22 pm

To modify several user accounts in bulk, you can use the export/import tools that s2Member Pro has. WP Admin -> s2Member -> Import/Export -> User

About chnaging the field's value with your script, get all the custom fields using get_user_option, look at how it's formatted, edit/add the value you want, and save with update_user_option.

https://codex.wordpress.org/Function_Re ... ser_option
https://codex.wordpress.org/Function_Re ... ser_option

Code: Select all
$s2_custom_fields = get_user_option('wp_s2member_custom_fields', $user_id);
print_r($s2_custom_fields); 


I hope that helps. :)
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Is it possible to change ccap value through API?

Postby slyhands » November 3rd, 2011, 12:58 pm

Heya Cristian -

I appreciate the quick reply and apologize for my additional question, due to my inexperience with Wordpress.

I have successful run the get_user_option as suggested above, which has returned the name/value pair I was looking for. So:

$s2_custom_fields = get_user_option('wp_s2member_custom_fields', 4);
print_r($s2_custom_fields);

returns:

Array ( [wall_of_heroes] => yes )


I understand that I must change/update the value utilizing:
update_user_option( $user_id, $option_name, $newvalue, $global )

So I tried this:
update_user_option(4,'wall_of_heroes','no');


However, it appears the value is unchanged. Am I doing something incorrectly?

Thanks again for any light you can shed on this.

Steve
User avatar
slyhands
Registered User
Registered User
 
Posts: 36
Joined: September 1, 2011

Re: Is it possible to change ccap value through API?

Postby Cristián Lávaque » November 4th, 2011, 12:09 am

You see, the value is the array you got first. So you edit the value in the array and update the field with that. Try this:

Code: Select all
<?php
$user_id 
4;
$s2_custom_fields get_user_option('wp_s2member_custom_fields'$user_id);
$s2_custom_fields['wall_of_heroes'] = 'no';
update_user_option($user_id'wp_s2member_custom_fields'$s2_custom_fields);
?>


I hope that helps. :)
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Is it possible to change ccap value through API?

Postby slyhands » November 8th, 2011, 2:44 pm

Heya Cristian -

Okay...it appears I am making progress...but I believe I may be changing the incorrect value from what s2member saves in the custom fields profile area.

I have the "Display your info on the wall of heroes" dropdown in the member profile area (possible value of "yes" or "no").

Here is the code that evaluates their profile to determine if their info should display or not:

Code: Select all
<?
$users = $wpdb->get_results ("SELECT `user_id` as `ID` FROM `" . $wpdb->usermeta . "` WHERE `meta_key` = '" . $wpdb->prefix . "s2member_custom_fields' AND `meta_value` REGEXP '.*\"wall_of_heroes\";s:[0-9]+:\"yes\".*'");
if (is_array ($users) && count ($users) > 0)
{
   foreach ($users as $user)
   {
      // Display user info
   }
}
 


If I go into the user profile and select "no" from the dropdown, the users info does NOT display on this page (which is correct). Changing it back to "yes" makes it show up.


So now I am attempting to modify that value via the code you provided. Before the loop runs, I tried this code:

Code: Select all
$user_id = 4;
$s2_custom_fields = get_user_option('wp_s2member_custom_fields', $user_id);
$s2_custom_fields['wall_of_heroes'] = 'no';
update_user_option($user_id, 'wp_s2member_custom_fields', $s2_custom_fields);
 


And here is what I am running in the loop:

Code: Select all
echo "<br>===============================================================<br>";
$user = /* Get full User object now. */ new WP_User ($user->ID);
$s2_custom_fields = get_user_option('wp_s2member_custom_fields', $user->ID);
print "NAME: <strong>" . $user->display_name . "</strong> | ID: " . $user->ID . " | Wall of Heroes: " . $s2_custom_fields['wall_of_heroes'];
echo "<br>===============================================================<br>";
 


Now for the strange part (to me!): For the user with ID 4 that I scripted to change the value to "no"...displays a value of "no" on the page.

However, since the initial query is only supposed to grab members that have a value of "yes"...I'm confused as to why that member is even showing up on this page? I also notice that if I login as the user and go to the profile page, the dropdown also indicates "yes", as if nothing has been changed. (If I change the value from the profile area and save, the name instantly disappears from the Wall of Heroes page as it should).

I suspect I am changing the incorrect value...I'm just not sure where I am going wrong based on how s2Member is saving the registration/profile fields.

Thanks in advance.

Steve
Last edited by Cristián Lávaque on November 11th, 2011, 12:13 am, edited 1 time in total.
Reason: Improve code readability. http://www.primothemes.com/forums/viewtopic.php?f=36&t=2780
User avatar
slyhands
Registered User
Registered User
 
Posts: 36
Joined: September 1, 2011

Re: Is it possible to change ccap value through API?

Postby Cristián Lávaque » November 11th, 2011, 12:26 am

Yeah, it's odd. I'm gonna ask Jason.
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Is it possible to change ccap value through API?

Postby slyhands » November 11th, 2011, 12:28 am

Thanks...I appreciate it!
User avatar
slyhands
Registered User
Registered User
 
Posts: 36
Joined: September 1, 2011

Re: Is it possible to change ccap value through API?

Postby Jason Caldwell » November 15th, 2011, 4:08 pm

Thanks for the heads up on this thread.

In this code snippet, you're injecting the wp_ prefix yourself, which is then prefixed again by WordPress, internally ( based on a broader configuration of WordPress itself ), resulting in your custom data being saved into a User's meta column, with a meta key that is NOT the same one that WordPress/s2Member uses. Thus, you are correct. The data is not being saved properly. It is actually going into a meta column with this invalid key: wp_wp_s2member_custom_fields

For example, this code is incorrect:
Code: Select all
$user_id = 4;
$s2_custom_fields = get_user_option('wp_s2member_custom_fields', $user_id);
$s2_custom_fields['wall_of_heroes'] = 'no';
update_user_option($user_id, 'wp_s2member_custom_fields', $s2_custom_fields); 
It should be done like this please:
Code: Select all
$user_id = 4;
$s2_custom_fields = get_user_option('s2member_custom_fields', $user_id);
$s2_custom_fields['wall_of_heroes'] = 'no';
update_user_option($user_id, 's2member_custom_fields', $s2_custom_fields); 


The only time you should use a prefix in your own code, is when/if you're making direct queries on the database, as seen in the example below. And, even then, you should always use the dynamic variable to obtain the prefix value, as this prefix changes, based on a broader configuration inside your /wp-config.php file. On a Multisite Network, it might change based on the child Blog's ID.

The default prefix on most WordPress installations is wp_. But you can't depend on that. It could be different, depending on the $table_prefix variable in your /wp-config.php file.

Always use $wpdb->prefix to obtain the prefix.
Code: Select all
<?
global $wpdb
;
$users = $wpdb->get_results ("SELECT `user_id` as `ID` FROM `" . $wpdb->usermeta . "` WHERE `meta_key` = '" . $wpdb->prefix . "s2member_custom_fields' AND `meta_value` REGEXP '.*\"wall_of_heroes\";s:[0-9]+:\"yes\".*'");
if (is_array ($users) && count ($users) > 0)
{
   foreach ($users as $user)
   {
      // Display user info
   }
}
?>
On most WordPress installations, the code above:
" . $wpdb->prefix . "s2member_custom_fields would translate to: wp_s2member_custom_fields. That's what you want.

But, you only need to worry about the prefix when you're making database queries like this. WordPress functions deal with this automatically. So when using get_user_option(), you just use: get_user_option("s2member_custom_fields", $user_id).

Related article: http://codex.wordpress.org/Installing_Multiple_Blogs
~ 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: Is it possible to change ccap value through API?

Postby Cristián Lávaque » November 15th, 2011, 7:11 pm

Thanks for that, sorry for missing it earlier.
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Is it possible to change ccap value through API?

Postby Jason Caldwell » November 15th, 2011, 7:24 pm

Sure, no problem Cristián.
This gets a lot of people. It's confusing I think, but does allow for some interesting configurations where different sets of tables, with different prefixes power multiple variations of a site, or completely different sites even. It is especially useful on a Multisite Network.
~ 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: Is it possible to change ccap value through API?

Postby Cristián Lávaque » November 15th, 2011, 10:32 pm

Yeah, I can see how that would be. Thanks. :)
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Is it possible to change ccap value through API?

Postby slyhands » November 17th, 2011, 4:39 pm

Jason -

Worked perfectly! Thank you for making that distinction. Knew we were close! hehe

Have a great one!

Steve
User avatar
slyhands
Registered User
Registered User
 
Posts: 36
Joined: September 1, 2011


Return to s2Member Plugin

Who is online

Users browsing this forum: Exabot [Bot] and 1 guest

cron