• Welcome to the all-new Kolakube support, the official place to get help for Marketers Delight, XFtoWP, CryptoWP, and other WordPress products! Please login with your account details to access members-only support forums.

How to sync XF usergroup to WP role?

ScarletCox

Member
XFtoWP
Messages
9
Reaction score
2
Am I overlooking something obvious?

I have a usergroup in XF. I would like all XF users in that group to be given a specific Role in WP.

Can someone point me at the magic bit in the middle that I can't find?

What I have done:
Added the XF group ID and name to the Usergroups tab in the Site Setup section of XFtoWP settings.
Enabled MemberPress Integration.
Created the custom Role in Members.
Imported all my XF users into WP.
....All the XF users got given the Subscriber WP role and nothing else.

Any idea what step I've missed?

Thanks :D
 
Hi @ScarletCox thank you for providing the steps to follow on this issue. XFtoWP doesn't change user roles on the WP side so all users are imported as Subscribers by default.

To be sure I understand, when you run a bulk import/refresh you want users who have a secondary usergroup (or primary?) in XenForo to be moved to a specified WP user role?

While this is not default functionality I'd be happy to write a small companion plugin to achieve exactly this if you need it. If there are any details past the above point let me know, like if you may want to repeat the process for other usergroups or if there any circumstances that may occur in the future where this same process would need to be automated.
 
Comment
I don't mind if it's added as a secondary WP user role :) They can stay in Subscribers, but it'd be useful to have an option to bulk-sync this secondary role too, if that's possible?

There would also need to be demotion functionality during synchronisation so that any XF user who has lost that role will have the secondary WP role removed too.

Can that be done?
 
Comment
To add potentially helpful specifics:

I use XF2's built-in Account Upgrades feature for paid subscriptions.

In addition to that there is an auto-promotion rule in place for subscribed members who then achieve certain posting goals. Achieving those posting goals uses XF's auto-promotion cron jobs to add a secondary user group to the user, but if the user subsequently unsubscribes, the cron job also removes the secondary user group.

I wish to constrain access to certain parts of the WP installation to WP users who have the role linked to that secondary XF user group, which means we need to be able to sync membership of it to the WP users too.

I don't mind manually hitting refresh in WP, but it would be far better if it were automated.

I hope that helps?
 
Comment
Hi @ScarletCox my apologies for falling off and not helping you sooner but I believe I have a solid solution that will help you out here. :)

Instead of a plugin, the simple code snippet below should help you achieve what you need to do here. Keep in mind the example below is only written for one usergroup example, so I may need to refactor it so you can more easily check multiple group combinations, but let me know.

The purpose of the code snippet below is to run after each time a WP user is synced/refreshed and check which XF usergroups are currently associated with the WP user. If a WP user gets any XF usergroup added or removed through a User action in the XFtoWP settings, this code snippet will check the usergroups and accordingly set a new WP user role.

Paste this into your child theme's functions.php file, or anywhere else you keep custom logic:

PHP:
/**
 * During the XF user sync process, check if specific usergroup(s)
 * exist and promote/demote WP user roles.
 *
 * @param int $wp_user_id The unique user ID of the WP user
 * @param array $wp_usermeta Saved user data about XF user in WP user
 * @since 1.0
 */

function scarlet_xfwp_sync_user_roles( $wp_user_id, $wp_usermeta ) {
    // If no XF user is synced, cancel code
    if ( empty( $wp_usermeta['main_account'] ) )
        return;

    // Set XF user variables
    $xf_user_id = $wp_usermeta['main_account'];
    $xf_user = $wp_usermeta['users'][$xf_user_id];
    $xf_usergroups = $xf_user['xf_secondary_group_ids'];

    // Change user roles of WP user_can_richedit()
    $wp_user = new WP_User( $wp_user_id );

    if ( ! empty( $xf_usergroups[5] ) )
        $wp_user->set_role( 'editor' );
    else
        $wp_user->set_role( 'subscriber' );
}

add_action( 'xfwp_before_user_update', 'scarlet_xfwp_sync_user_roles', 10, 2 );

Take a look at the lines towards the end of the function, which is set as a simple if statement. The $xf_usergroups variable is a list of XF usergroups the WP user knows, and 5 is the ID of the XF usergroup.

The if statement reads "If the user is in XF usergroup 5, assign them to be the Editor role in WP. If they are not in XF usergroup, set them back to the Subscriber role."

You can test this snippet as simply as editing the WP user, checking or unchecking the XF usergroup, press "Sync user" and refresh the page to see the WP user role field at the top of the page has changed.

Screen Shot 2023-07-20 at 21.18.29-fullpage.png

Please let me know how this goes for you. :)
 
Comment
Back
Top