latest stable versions: v150827 (changelog)

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Updating a Custom Field Value

Home Forums Community Forum Updating a Custom Field Value

This topic contains 14 replies, has 3 voices. Last updated by  Gary 3 years, 5 months ago.

Topic Author Topic
Posted: Friday Aug 2nd, 2013 at 3:02 pm #54782
Gary
Username: gbthomas66

Is there a way to update the value of a s2member custom field? Here is my scenario:

I’m trying to run an election for our members organization. I have a custom fields that indicates if they have cast their vote already or not (0 or 1). At the beginning of the election, everyone’s flag will be set to “0”.

When they go to the voting page, they will see a blank ballot if their flag is set to “0”, or a message that says they’ve already voted if the flag is a “1”.

If they haven’t voted, they do so by submitting a form. At this point, I want to rewrite their custom field to be a “1” instead of a “0”.

How do I do this? Thanks!

List Of Topic Replies

Viewing 14 replies - 1 through 14 (of 14 total)
Author Replies
Author Replies
Posted: Saturday Aug 3rd, 2013 at 2:33 am #54822
Bruce
Username: Bruce
Staff Member

Is there a way to update the value of a s2member custom field?

Sure, but it’s a bit complicated. To update an s2Member Custom Field you have to update an array in your WordPress Usermeta tables through PHP. The value’s row is your WordPress prefix + s2member_custom_fields. Here’s an example of how you would update a Custom Field with the slug country:

$custom_fields = get_user_option('s2member_custom_fields');
$custom_fields['country'] = $newcountry;
update_user_option($user->ID, 's2member_custom_fields', $custom_fields);
Posted: Saturday Aug 3rd, 2013 at 1:33 pm #54835
Gary
Username: gbthomas66

Thanks Bruce. That doesn’t look too complicated. I may not get back to it for a couple of days. Just wanted to acknowledge getting the info.

Posted: Saturday Aug 3rd, 2013 at 10:10 pm #54974
Gary
Username: gbthomas66

Well, I got to working on it tonight. But it isn’t doing the update. Here’s my code:

$voted = 1;
$custom_fields = get_user_option('aaa_s2member_custom_fields');
$custom_fields['district_voted'] = $voted;
update_user_option($user->ID, 'aaa_s2member_custom_fields', $custom_fields);

The first three lines of code work correctly (confirmed by echo and print_r).
The last line executes, but gives me no error, nor does it update the custom field “district_voted”.
Any thoughts?

Posted: Saturday Aug 3rd, 2013 at 10:18 pm #54977
Bruce
Username: Bruce
Staff Member
$voted = 1;
$custom_fields = get_user_option('aaa_s2member_custom_fields');
$custom_fields['district_voted'] = $voted;
update_user_option($user->ID, 'aaa_s2member_custom_fields', $custom_fields);

You don't need to put the prefix in here if you're using update_user_option(). WordPress does this automatically. Try this:

$voted = 1;
$custom_fields = get_user_option('s2member_custom_fields');
$custom_fields['district_voted'] = $voted;
update_user_option($user->ID, 's2member_custom_fields', $custom_fields);
Posted: Sunday Aug 4th, 2013 at 10:29 pm #55184
Gary
Username: gbthomas66

Still no luck. Again, the $custom_fields array looks fine before and after the change to the one field. But the last line does not seem to write it to the database.

Posted: Monday Aug 5th, 2013 at 12:27 am #55202
Moderator

I would only add that you should be specific about which User you’re pulling data for here, by passing the $user->ID to the get_user_option() call also; as seen below.

$voted = 1;
$custom_fields = get_user_option('s2member_custom_fields', $user->ID);
$custom_fields['district_voted'] = $voted;
update_user_option($user->ID, 's2member_custom_fields', $custom_fields);
Posted: Monday Aug 5th, 2013 at 6:22 pm #55245
Gary
Username: gbthomas66

Thanks for the added input. I wish I could say it is now working, but unfortunately it still is not updating the values. It’s like it is ignoring that last line of code. It must be something else. Perhaps a permission problem somewhere? Any other thoughts or ideas? I appreciate the help.

Posted: Monday Aug 5th, 2013 at 6:25 pm #55246
Moderator

This probably has more to do with the larger picture.
~ Where/how is this code integrated? It is attached to a hook?

Posted: Monday Aug 5th, 2013 at 6:46 pm #55247
Gary
Username: gbthomas66

I use the plug-in Exec-PHP. It allows me to use PHP code in any page or post. I’ve never had any issue with it until this particular one.

Posted: Monday Aug 5th, 2013 at 7:24 pm #55248
Moderator

And what page is it on? Does it come before or after anything else that processes profile fields?

Posted: Monday Aug 5th, 2013 at 11:14 pm #55266
Gary
Username: gbthomas66

There is no other processing custom fields with my PHP code on this page. But I suppose something could be happening behind the scenes. The page is a protected page that you can’t access.

Posted: Tuesday Aug 6th, 2013 at 7:19 am #55278
Moderator

Yes, that’s what I would start looking at because your code is rock solid there. Something is happening after this code, and it’s probably tricking you into thinking there is something wrong with that code snippet. It looks good :-) It’s most likely something else that comes after this.

Posted: Tuesday Aug 6th, 2013 at 10:54 am #55291
Gary
Username: gbthomas66

Thanks again for your help. I think my next step will be to pull this PHP code out of the WP page and have it be a standalone PHP page that gets called from a page. Hopefully that will solve the problem.

Posted: Friday Aug 9th, 2013 at 3:50 pm #55500
Gary
Username: gbthomas66

I finally figured it out!!! It was related to the ID field after all. I changed the code to this:

$voted = "1";
$custom_fields = get_user_option('s2member_custom_fields', get_current_user_id());
$custom_fields['district_voted'] = $voted;
update_user_option(get_current_user_id(), 's2member_custom_fields', $custom_fields);
Viewing 14 replies - 1 through 14 (of 14 total)

This topic is closed to new replies. Topics with no replies for 2 weeks are closed automatically.

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Contacting s2Member: Please use our Support Center for bug reports, pre-sale questions & technical assistance.