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™

How to update a custom field?

s2Member Plugin. A Membership plugin for WordPress®.

How to update a custom field?

Postby talkdownunder » June 1st, 2011, 2:08 am

Hi,

I want to update 2 custom fields.
One to check if user is login for the first time, then change the value to 0.
Second is the age, a cron job runs every midnight to update this field.

- I'm struggling to find a way to update a custom field. Can anyone shed a light please.
- And if I write a simple PHP script for the cron, will it work with this s2Member custom filed data structure? Should I use any special method for this.

Please help. Spent many hours on this already. Quick help much appreciated.

Cheers
Nad
User avatar
talkdownunder
Registered User
Registered User
 
Posts: 19
Joined: May 17, 2011

Re: How to update a custom field?

Postby Cristián Lávaque » June 1st, 2011, 10:15 am

Hi Nad.

You could have the field be the birthdate instead of age, then you'd figure out the age any time you need it without having to edit the custom field's value.

About the first login, you can get the s2Member custom fields in an array with

Code: Select all
$s2_custom_fields = get_user_option('s2member_custom_fields', $user_id); 
* If you don't give it the user ID, it'll default to the current user.

Check the $s2_custom_fields['firlst_login'] and if true update the value to false. Try the update_user_option WP function. https://codex.wordpress.org/Function_Re ... ser_option
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: How to update a custom field?

Postby adamarling » June 1st, 2011, 1:44 pm

Hi, I'm having the same issue.

Through my own custom PHP script, I'd like to update the value of a Custom Field ('b_city'). Here's my code:

Code: Select all
$user_id = 6;
update_user_meta($user_id, 'b_city', 'New City Name');


and I've also tried:

Code: Select all
update_user_option($user_id, 'b_city', 'New City Name')


...however neither works. Is there another way I should refer to the Custom Field title, or am I using the wrong function? I can't find answers anywhere for the life of me.

thanks,
Adam
User avatar
adamarling
Registered User
Registered User
 
Posts: 27
Joined: May 12, 2011

Re: How to update a custom field?

Postby Cristián Lávaque » June 1st, 2011, 3:36 pm

Look at what get_user_option returns and follow that format when you update_user_option. Let me know if 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: How to update a custom field?

Postby talkdownunder » June 1st, 2011, 6:24 pm

No luck Cristan, this is dragging for the second day now :roll:
This code returns the same value for "first_time" custom field, means "update_user_option" didn't work. Soo frustrated now. Much appreciated your help.

Code: Select all
$s2_custom_fields = get_user_option('s2member_custom_fields', $user_id);
echo "first time value is:".$s2_custom_fields['first_time']."<br/>";

update_user_option($user_id, 'first_time', 'No');   

$s2_custom_fields = get_user_option('s2member_custom_fields', $user_id);
echo "Now first time value is:".$s2_custom_fields['first_time'];


-
For that age calculation, I need to display age in the user edit screen. If I do that manipulating user edit page, WP updates will replace that and I loose the changes I made right? So only option I could see having a cron to update custom field "age". Any other suggestions much appreciated.

Cheers
Nad
User avatar
talkdownunder
Registered User
Registered User
 
Posts: 19
Joined: May 17, 2011

Re: How to update a custom field?

Postby Cristián Lávaque » June 1st, 2011, 9:15 pm

You could print_r($s2_custom_fields) to see what it's like, and make sure you edit it properly before updating it, and read the update function documentation. ;)

Here's your example a bit edited:

Code: Select all
$s2_custom_fields = get_user_option('s2member_custom_fields', $user_id);
echo 'First time value is: ' . ($s2_custom_fields['first_time'] ? 'true' : 'false') . '<br/>';

$s2_custom_fields['first_time'] = false;
update_user_option($user_id, 's2member_custom_fields', $s2_custom_fields);

$s2_custom_fields = get_user_option('s2member_custom_fields', $user_id);
echo 'Now first time value is: ' . ($s2_custom_fields['first_time'] ? 'true' : 'false') . '<br/>';
 


I suggest using true or false for your field, instead of yes or no.

I hope it 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: How to update a custom field?

Postby adamarling » June 2nd, 2011, 10:15 am

Christian,

Ahhhh, now I see how to update a Custom Field via the proper syntax of 'update_user_option'. Thank you so much. Works perfectly. I have a lot of Custom Fields in my site, and an entire separate Admin area (outside of WordPress), so it's great to see how the two tie together. You the man.

- Adam
User avatar
adamarling
Registered User
Registered User
 
Posts: 27
Joined: May 12, 2011

Re: How to update a custom field?

Postby Cristián Lávaque » June 2nd, 2011, 1:45 pm

Thanks, Adam! Glad to help. :)
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: How to update a custom field?

Postby talkdownunder » June 2nd, 2011, 7:05 pm

Yes its working for me too.
Well... thanks soo much for helping me out mate.

Cheers
Nad
User avatar
talkdownunder
Registered User
Registered User
 
Posts: 19
Joined: May 17, 2011

Re: How to update a custom field?

Postby Cristián Lávaque » June 2nd, 2011, 7:45 pm

Awesome, Nad! I'm glad it is. :)
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


Return to s2Member Plugin

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests

cron