Community Support Forums — WordPress® ( Users Helping Users ) — 2011-11-17T16:39:30-05:00 http://www.primothemes.com/forums/feed.php?f=4&t=15753 2011-11-17T16:39:30-05:00 http://www.primothemes.com/forums/viewtopic.php?t=15753&p=53367#p53367 <![CDATA[Re: Is it possible to change ccap value through API?]]>
Worked perfectly! Thank you for making that distinction. Knew we were close! hehe

Have a great one!

Steve

Statistics: Posted by slyhands — November 17th, 2011, 4:39 pm


]]>
2011-11-15T22:32:44-05:00 http://www.primothemes.com/forums/viewtopic.php?t=15753&p=53145#p53145 <![CDATA[Re: Is it possible to change ccap value through API?]]>

Statistics: Posted by Cristián Lávaque — November 15th, 2011, 10:32 pm


]]>
2011-11-15T19:24:12-05:00 http://www.primothemes.com/forums/viewtopic.php?t=15753&p=53134#p53134 <![CDATA[Re: Is it possible to change ccap value through API?]]> 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.

Statistics: Posted by Jason Caldwell — November 15th, 2011, 7:24 pm


]]>
2011-11-15T19:11:50-05:00 http://www.primothemes.com/forums/viewtopic.php?t=15753&p=53131#p53131 <![CDATA[Re: Is it possible to change ccap value through API?]]> Statistics: Posted by Cristián Lávaque — November 15th, 2011, 7:11 pm


]]>
2011-11-15T16:08:35-05:00 http://www.primothemes.com/forums/viewtopic.php?t=15753&p=53099#p53099 <![CDATA[Re: Is it possible to change ccap value through API?]]> 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:
$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:
$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:
<?
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

Statistics: Posted by Jason Caldwell — November 15th, 2011, 4:08 pm


]]>
2011-11-11T00:28:43-05:00 http://www.primothemes.com/forums/viewtopic.php?t=15753&p=52745#p52745 <![CDATA[Re: Is it possible to change ccap value through API?]]> Statistics: Posted by slyhands — November 11th, 2011, 12:28 am


]]>
2011-11-11T00:26:13-05:00 http://www.primothemes.com/forums/viewtopic.php?t=15753&p=52744#p52744 <![CDATA[Re: Is it possible to change ccap value through API?]]> Statistics: Posted by Cristián Lávaque — November 11th, 2011, 12:26 am


]]>
2011-11-08T14:44:27-05:00 http://www.primothemes.com/forums/viewtopic.php?t=15753&p=52564#p52564 <![CDATA[Re: Is it possible to change ccap value through API?]]>
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:
<?
$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:
$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:
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

Statistics: Posted by slyhands — November 8th, 2011, 2:44 pm


]]>
2011-11-04T00:09:21-05:00 http://www.primothemes.com/forums/viewtopic.php?t=15753&p=50976#p50976 <![CDATA[Re: Is it possible to change ccap value through API?]]>
Code:
<?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. :)

Statistics: Posted by Cristián Lávaque — November 4th, 2011, 12:09 am


]]>
2011-11-03T12:58:05-05:00 http://www.primothemes.com/forums/viewtopic.php?t=15753&p=50926#p50926 <![CDATA[Re: Is it possible to change ccap value through API?]]>
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

Statistics: Posted by slyhands — November 3rd, 2011, 12:58 pm


]]>
2011-11-02T23:22:10-05:00 http://www.primothemes.com/forums/viewtopic.php?t=15753&p=50859#p50859 <![CDATA[Re: Is it possible to change ccap value through API?]]> 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:
$s2_custom_fields = get_user_option('wp_s2member_custom_fields', $user_id);
print_r($s2_custom_fields); 


I hope that helps. :)

Statistics: Posted by Cristián Lávaque — November 2nd, 2011, 11:22 pm


]]>
2011-11-02T12:22:44-05:00 http://www.primothemes.com/forums/viewtopic.php?t=15753&p=50798#p50798 <![CDATA[Is it possible to change ccap value through API?]]>
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

Statistics: Posted by slyhands — November 2nd, 2011, 12:22 pm


]]>