Hi,
Further to this – I’ve discovered that when you have the plugin set to sync only a specific role and then you change a user to another role it does not seem to sync back to mailchimp and unsubscribe them from the list.
Is this deliberate, as it doesn’t seem to be intuitive?
Thanks.
Hey meglos42,
Sorry about that, let’s see that we get your problem fixed in this topic then! (For future reference, meglos42’s previous topic can be found here)
I’ll request for Danny to take a look at your topic. He’ll reply to you a.s.a.p.!
I’ve been following the chain of threads, and thought I’d add that some trigger to be able to unsubscribe users would be great! Your plugin is pretty much exactly what I’ve been needing, but I’m not able to make it live until I have a way for users to opt-out and unsubscribe manually somehow.
+1 for this.
If a new user starts out as ‘Inactive’ and then changed to ‘Subscriber’, the MailChimp list is updated and the user is added to the list.
However, subsequent changing of a user’s role does not subscribe/unsubscribe from the MailChimp list accordingly.
Possible to fix?
Hi – not heard anything back for a while, is this getting looked at?
If the plugin can subscribe/unsubscribe from the MailChimp list when the user’s role is changed it would make it much more useful.
Hey all,
Sorry for the late reply – crazy busy working on our set of MailChimp plugins.
Your request makes sense, but requires a bit of more work with the current way things are set-up. Basically the premise is that the plugin only unsubscribes sparingly since we don’t want to make any unnecessary (negative) changes to your MailChimp lists.
I’m a bit short on time right now but I’ll update this topic later this week with some code to do this manually before we add this to the plugin itself.
Please bear with me – I’ll get to it. Promise. 🙂
Thanks Danny,
So, will it be possible to automatically change the user’s Mailchimp subscription state when the value of the field “active” on the xx_usermeta table changes from a 0 to a 1 (or vice versa)? The field is added by the wp_members plugin.
This would negate the necessity for controlling subscription/un-subscription via user roles.
Hey all,
I’ve been giving this some more thought and will update the plugin later with code that handles this, although I have to say that it is hard to come up with a solution that would work for all because of the many different variables at play here.
Here’s some code that will “watch” the user role and tell the MailChimp Sync plugin to unsubscribe a given user when it no longer has the required role.
add_action( 'updated_user_meta', function( $meta_id, $user_id, $meta_key, $meta_value ) {
$required_role = 'subscriber';
$user = get_userdata( $user_id );
// Check if user still has required role
if( ! in_array( $required_role, $user->roles ) ) {
// user does not have role, tell MailChimp User Sync to unsubscribe user.
do_action( 'mailchimp_sync_unsubscribe_user', $user_id );
}
}, 10, 4 );
You could modify this code to check for the “active” field and whether it changed to do what you want it to do.
There are multiple actions you can tell MailChimp Sync to do.
mailschimp_sync_update_subscriber (updates a subscriber or creates a new subscriber for the user)
mailchimp_sync_unsubscribe_user (unsubscribes the user)
mailchimp_sync_subscribe_user (subscribes a user, ignoring whether he was subscribed already)
That should help as a starting point to get the plugin to do what you want. I’ll think of a way to make this easier from the plugin interface.
Thanks Danny, I’ll give that a whirl just now and let you know how it goes.
Hi Danny, this is working fine for subscribing/unsubscribing users as their role changes. I can’t seem to get it working for the change in meta_value of the meta_key “active” though (probably due to my imperfect understanding of the way the code works). The code I have is –
add_action( 'updated_user_meta', function( $meta_id, $user_id, $meta_key, $meta_value ) {
$active_value = 1;
$user = get_userdata( $user_id );
// Check if user is active or not
if( ! in_array( $active_value, $user->active ) ) {
// user is not active, tell MailChimp User Sync to unsubscribe user.
do_action( 'mailchimp_sync_unsubscribe_user', $user_id );
}
}, 10, 4 );
Can you help?
Edit: also tried
if ( ! ($active_value == $user->active) ) {
with no joy
Hi Danny,
Would you be able to give me some advice on the correct syntax to use for this, as I’m still struggling?
Many thanks.
Hi,
Can anyone give me advice on the correct syntax to use here please?
Thanks.
Hi – Why is is this marked as resolved?
I still don’t have a working solution for syncing only when the value of the field “active” on the wp_usermeta table changes from a 0 to a 1 and vice versa.
I’ve tried the code above with no success.
At the moment I am having to sync users using a user role, which is not ideal.
Hi Meglos,
Sorry, this slipped through my attention. Could you please give the following code a try?
add_action( 'updated_user_meta', function( $meta_id, $user_id, $meta_key, $meta_value ) {
if( $meta_key === 'active' && $meta_value != 1 ) {
// user just switched to inactive, tell MailChimp User Sync to unsubscribe user.
do_action( 'mailchimp_sync_unsubscribe_user', $user_id );
}
}, 10, 4 );
This will only fire the unsubscribe call when the meta value for active changes to anything other than key. Otherwise, the code would be firing A LOT of unsubscribe calls repeatedly for any change in an inactive user.
Hope that helps. If not, let me know!
Thanks Danny, I’ll try this today.
Will this also subscribe a newly created user if the meta value changes from 0 to 1?
At present new users are by default set to inactive (0) and we only want them to get subscribed & start receiving mails if they are activated.
Similarly, if they are de-activated they should be unsubscribed.