• Resolved markus9312

    (@markus9312)


    Hi there. I’m looking to replace Mailchimp, as integrating user roles between WordPress & Mailchimp has become a real pain, with lots of manual work involved. I was hoping The newsletter plugin could help me with this.

    So what I’m looking for is to sync 6 different WordPress-roles, to 6 corresponding lists. If a user has “X” role, they should be added to “X” list. It should also be removed if the user gets another role.

    Is this possible? I tried using the WP user integration addon – and while that did import all users, it didn’t add them to a list. I checked through the documentation here: https://www.thenewsletterplugin.com/documentation/addons/extended-features/wpusers-extension/ but I fell flat.

    I’ll happily do a custom solution, as long as there is some documentation covering this topic. Any suggestions?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Stefano Lissa

    (@satollo)

    Hi, the wp users addon does not provide such feature. You should code a custom solution: wp users is changed, the role is extracted, the connected subscriber is loaded using the plugin function ($subscriber = Newsletter::instance()->get_user_by_wp_user_id(…)), update the subscriber to have the correct list based on the wp user: $subscriber->list_x = 1. Then the subscriber needs to be saved Newsletter::instance()->save($subscriber).

    Thread Starter markus9312

    (@markus9312)

    Thank you so much @satollo ! Could you tell me if I’m on the right path?

    To get all current users in the correct role (not just ones that are updated), I did a a dry run of all the current users & looped through them, with a conditional for each one of the roles I want added?

    //With proper args so that all users are queried, currently missing this
    $user_query = new WP_User_Query( $args );
    
    foreach ($user_query as $user){
    
    $subscriber = Newsletter::instance()->get_user_by_wp_user_id($user->id))
    
    //Role query from https://wordpress.stackexchange.com/a/5048  
    if ( in_array( 'custom-role1', (array) $user->roles ) ){ 
    $subscriber->custom_list1 = 1; 
    }
    
    if ( in_array( 'custom-role2', (array) $user->roles ) ){ 
    $subscriber->custom_list2 = 1; 
    }
    
    Newsletter::instance()->save($subscriber);
    return;
    } 

    And when checking if an user is changed adjust the query and instead just do a check for when the profile is updated?

    
    
    //From https://developer.www.ads-software.com/reference/hooks/profile_update/#comment-4988
    function wpdocs_check_user_email_updated( $user_id ) {
     
    
    $subscriber = Newsletter::instance()->get_user_by_wp_user_id($user_id))
    
    //Role query from https://wordpress.stackexchange.com/a/5048  
    if ( in_array( 'custom-role1', (array) $user->roles ) ){ 
    $subscriber->custom_list1 = 1; 
    }
    
    if ( in_array( 'custom-role2', (array) $user->roles ) ){ 
    $subscriber->custom_list2 = 1; 
    }
    
    Newsletter::instance()->save($subscriber);
    return;
    } 
    
    add_action( 'profile_update', 'wpdocs_check_user_email_updated', 10, 2 );
    
    Plugin Author Stefano Lissa

    (@satollo)

    I suggest to “reset” the lists before setting them accordingly the user roles:

    $subscriber->list_1 =0;

    $subscriber->list_2 =0;

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Syncing user roles with a list’ is closed to new replies.