Page 1 of 1

How to update a custom field?

PostPosted: June 1st, 2011, 2:08 am
by talkdownunder
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

Re: How to update a custom field?

PostPosted: June 1st, 2011, 10:15 am
by Cristián Lávaque
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

Re: How to update a custom field?

PostPosted: June 1st, 2011, 1:44 pm
by adamarling
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

Re: How to update a custom field?

PostPosted: June 1st, 2011, 3:36 pm
by Cristián Lávaque
Look at what get_user_option returns and follow that format when you update_user_option. Let me know if that helps.

Re: How to update a custom field?

PostPosted: June 1st, 2011, 6:24 pm
by talkdownunder
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

Re: How to update a custom field?

PostPosted: June 1st, 2011, 9:15 pm
by Cristián Lávaque
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. :)

Re: How to update a custom field?

PostPosted: June 2nd, 2011, 10:15 am
by adamarling
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

Re: How to update a custom field?

PostPosted: June 2nd, 2011, 1:45 pm
by Cristián Lávaque
Thanks, Adam! Glad to help. :)

Re: How to update a custom field?

PostPosted: June 2nd, 2011, 7:05 pm
by talkdownunder
Yes its working for me too.
Well... thanks soo much for helping me out mate.

Cheers
Nad

Re: How to update a custom field?

PostPosted: June 2nd, 2011, 7:45 pm
by Cristián Lávaque
Awesome, Nad! I'm glad it is. :)