Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KaineLabs/9f298b9d6baf93ae289e875f4a737b64 to your computer and use it in GitHub Desktop.
Save KaineLabs/9f298b9d6baf93ae289e875f4a737b64 to your computer and use it in GitHub Desktop.
Youzify - BuddyPress Assign Existing Users with Specific Roles to Specific Member Type
<?php
function yzc_get_linked_member_types_and_user_roles_2() {
// Set Member Type Name => User Role
return array(
'teacher' => 'subscriber',
'type2' => 'editor'
);
}
add_action( 'bp_init' ,'yzc_update_all_profile_membertypes' );
function yzc_update_all_profile_membertypes() {
if ( get_option( 'yzc_update_all_profile_membertypes' ) ) {
return;
}
// Get Member Types and their Users Roles
$member_roles = yzc_get_linked_member_types_and_user_roles_2();
$member_types = array_flip( $member_roles );
// Get all users.
$authors = get_users( array( 'fields' => 'id' ) );
// Update Fields.
foreach ( $authors as $user_id ) {
$user = get_userdata( $user_id );
foreach($member_roles as $member_role) {
if ( in_array( $member_role, (array) $user->roles ) ) {
bp_set_member_type( $user_id, $member_types[ $member_role ] );
}
}
}
update_option( 'yzc_update_all_profile_membertypes', 1 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment