Skip to content

Instantly share code, notes, and snippets.

@pbrocks
Last active November 7, 2024 04:00
Show Gist options
  • Save pbrocks/bae0b08b11d5832884900e5b83e505bd to your computer and use it in GitHub Desktop.
Save pbrocks/bae0b08b11d5832884900e5b83e505bd to your computer and use it in GitHub Desktop.
Add a dropdown for bbPress role to PMPro's Add a Member screen
<?php
/**
* Adds dropdown for bbPress user role with Moderator and Participant as options
*/
add_action( 'pmpro_add_member_fields', 'add_bbpress_role_to_add_member' );
add_action( 'pmpro_add_member_added', 'pmpro_add_forum_role', 11 );
function add_bbpress_role_to_add_member( $profileuser ) {
global $wp_roles;
// Bail if current user cannot edit users
if ( ! current_user_can( 'edit_user', $profileuser->ID ) ) {
return;
}
// Get the roles
$dynamic_roles = bbp_get_dynamic_roles();
unset( $dynamic_roles[ bbp_get_keymaster_role() ] );
unset( $dynamic_roles[ bbp_get_moderator_role() ] );
unset( $dynamic_roles[ bbp_get_blocked_role() ] );
?>
<tr>
<th><label for="bbp-forums-role"><?php esc_html_e( 'bbPress Forum Role', 'pmpro-bbpress' ); ?></label></th>
<td>
<?php $user_role = bbp_get_user_role( $profileuser->ID ); ?>
<select name="bbp-forums-role" id="bbp-forums-role">
<?php if ( ! empty( $user_role ) ) : ?>
<option value=""><?php esc_html_e( '&mdash; No role for these forums &mdash;', 'pmpro-bbpress' ); ?></option>
<?php else : ?>
<option value="" selected="selected"><?php esc_html_e( '&mdash; No role for these forums &mdash;', 'pmpro-bbpress' ); ?></option>
<?php endif; ?>
<?php foreach ( $dynamic_roles as $role => $details ) : ?>
<option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo bbp_translate_user_role( $details['name'] ); ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<?php
}
function pmpro_add_forum_role( $user_id ) {
$user_role = 'subscriber';
$bbp_role = 'bbp_participant';
if ( is_wp_error( $user_id ) ) {
echo $return->get_error_message();
}
if ( ! empty( $_POST['role'] ) ) {
$user_role = $_POST['role'];
}
if ( ! empty( $_POST['bbp-forums-role'] ) ) {
$bbp_role = $_POST['bbp-forums-role'];
}
// $capabilities_item[] = $bbp_role;
$capabilities_array = array(
$user_role => true,
$bbp_role => true,
);
$updated_roles = update_user_meta( $user_id, 'wp_capabilities', $capabilities_array );
}
@shiningapostar
Copy link

add_action( 'pmpro_add_member_fields', 'add_bbpress_role_to_add_member' ); add_action( 'edit_user_profile_update', 'add_bbpress_role_to_add_member' ); function add_bbpress_role_to_add_member( $profileuser ) { global $wp_roles;

hi, how to use this? can you explain for me?

@gabriel-lev-t
Copy link

Hi David,

Could you tell me how to run this file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment