Home › Forums › Community Forum › Problems updating custom fields via a filter
This topic contains 9 replies, has 4 voices. Last updated by Jason (Lead Developer) 4 years ago.
Topic Author | Topic |
---|---|
Posted: Friday Dec 14th, 2012 at 7:54 am #34649 | |
|
|
We’re trying to add some metadata to user accounts which we can pass through to Mailchimp, but we’re having difficulty working out exactly how to make the updates stick on the WP side. At the moment, we’ve got code attached to the ws_plugin__s2member_mailchimp_merge_array filter, and, to summarise it does the following:
And although the correct data is sent to Mailchimp, it doesn’t stick in the WP user record. Within the filter, if we call get_user_option() again after calling update_user_option(), it returns the updated information. But when we go back to the list of users in the WP admin interface, it’s still got the old values. Have we missed something? Thanks. |
List Of Topic Replies
Author | Replies |
---|---|
Author | Replies |
Posted: Monday Dec 17th, 2012 at 7:57 pm #34872 | |
![]() |
|
Hi Andy, Sorry about the delay. Can you post the entire code snippet that you’re using? |
|
Posted: Tuesday Dec 18th, 2012 at 4:35 am #34886 | |
|
|
Here’s the full code (note the verification I mentioned in the original post is all commented out):
|
|
Posted: Wednesday Dec 19th, 2012 at 8:05 am #35023 | |
|
|
[hilite pre_code]
$today = date('Y-m-d'); // yyyy-mm-dd if ($cFields['course'] == '') { $cFields['course'] = 'Faculty'; $needUpdate = true; } if ($cFields['start_date'] == '' || strtotime($cFields['start_date']) < strtotime($today)) { // Start date is blank or in the past - bring it to today $cFields['start_date'] = $today; // yyyy-mm-dd $needUpdate = true; } if ($needUpdate) { update_user_option($uid, 's2member_custom_fields', $cFields); // echo 'fields updated '; } [/hilite] The “course” field could be defaulted to Faculty in your definition of the field. [hilite path]Dashboard -› s2Member® -› General Options -› Registration/Profile Fields[/hilite] The “date” part I’d probably do like this: [hilite pre_code] |
|
Posted: Thursday Dec 20th, 2012 at 6:53 am #35131 | |
|
|
Re the default fields – those options only seem to take effect if the field is visible to the user. This is pure metadata and is set to Invisible during and after registration. The default value is set in the options, but comes in as an empty string in this filter. Re the date – thanks, I’ll change that to call empty(). Out of interest, what’s the advantage of a second call to date() in the assignment, rather than just using $today, which is already assigned? More importantly, yes, the output included ‘fields updated’. To test, I created a user account with a ‘Start Date’ of 2012-12-19 and role of ‘Subscriber’. I then promoted that user to ‘s2 Level 1’ – here’s the full output when the debugging ‘echos’ are uncommented:
What’s really odd is the last call to get_user_option() uses a different array ($dField[]) and does load the correct data. But it only seems to persist like that until the end of the function. The userlist on WP still shows 2012-12-19 for that user. I can change the ‘Start Date’ from the Edit User page on WP, and then it does stick. |
|
Posted: Saturday Dec 22nd, 2012 at 8:01 am #35308 | |
|
|
Okay.
Ah, I didn’t even define [hilite mono]$today[/hilite], since it’d only be used in a single place.
Yes, that’s strange… Do you have object/database caching? |
|
Posted: Saturday Dec 22nd, 2012 at 9:01 am #35313 | |
|
|
Hmm – $today is defined in the existing script, and used in the date comparison (strtotime($today)) in the if statement. Just thought it sensible to do that rather than make 2 calls to date(). Re caching – do you mean a plugin doing that? If so, no – just standard WP. Here’s a full list of the plugins we’re using: amr users I don’t think any of them do any caching. Also, I was just looking at the WP codex, and the notes for update_user_option() say this:
This probably explains why I couldn’t get anything to work when I originally had the code using ‘wp_s2member_custom_fields’ – it would have ended up as ‘wp_wp_s2member_custom_fields’! But from what I can see of that function, it should be working. Is there anything else I can check – on the db side for example? Thanks, David. |
|
Posted: Thursday Jan 3rd, 2013 at 8:29 am #36100 | |
![]() |
|
Thanks for the heads up on this thread.
It sounds to me like you’re using this filter for MailChimp, to not only affect the MailChimp MERGE vars, but also to update a user’s record at the same time. While it’s certainly possible to do this, I would’nt do it all inside a filter attaching to The reason you’re seeing details properly while in your custom function that’s working together with this filter, but they’re NOT sticking, is because WordPress and s2Member are actually still in the process of registering/updating the user’s account record with a completely different set of information, which does not include the custom data you’re attempting to save upon hooking into this filter. So the solution in a case like this, is to use the MailChimp filter only for what it’s designed for. Then use a Hook provided by WordPress or s2Member to actually save the data to the user’s account record, at the appropriate time (i.e. using a Hook that puts you right in the action, where WordPress/s2Member are actually handling the customer’s details). I recommend this hook to save user data during registration.
|
|
Posted: Thursday Jan 3rd, 2013 at 9:18 am #36108 | |
|
|
Thanks for the analysis and advice – I would certainly agree we are trying to do more than the mailchimp filter is designed for, and so separating that code out makes sense. The only concern I now have is that those user fields are needed on the mailchimp side as well, so we need to be sure the custom fields are updated before any calls to the mailchimp api. I presume things would happen in the right order, but do you know for sure if ws_plugin__s2member_during_configure_user_registration_front_side is called before ws_plugin__s2member_mailchimp_merge_array? In general, is there an easy way to know in what order these filters are applied? Thanks again! PS – off topic: I can’t find the tag to put those filter names in ‘code hilite inline’ format as you did – the code tags always create a block for me – is there a trick? Tx! |
|
Posted: Saturday Jan 5th, 2013 at 1:28 am #36220 | |
![]() |
|
The code sample and registration Hook I provided (
We work with a slightly different set of tools than our customer’s do here. However, you can accomplish something similar using ~ Test with inline code sample using a tt tag. Customers can do this. |
This topic is closed to new replies. Topics with no replies for 2 weeks are closed automatically.