Skip to content

Instantly share code, notes, and snippets.

@raviousprime
Created August 29, 2019 14:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raviousprime/ea52f4b1ebb531731387a70eb95e91c8 to your computer and use it in GitHub Desktop.
Save raviousprime/ea52f4b1ebb531731387a70eb95e91c8 to your computer and use it in GitHub Desktop.
Bulk join user of certain role to BuddyPress group when created
<?php
add_action( 'groups_create_group', function( $group_id ) {
// Get users of certain roles.
$user_ids = get_users(
array(
'role' => array( 'subscriber' ),
'fields' => 'ID',
)
);
if ( empty( $user_ids ) ) {
return;
}
foreach ( $user_ids as $user_id ) {
// Add them to group.
groups_join_group( $group_id, $user_id );
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment