Wordpress - Change forum role from outside bbPress

2019-05-27 11:14发布

I would like to check the user status and update their bbPress forum role accordingly. (Not the Wordpress role.)

The purpose is to add functionality to the BP-Registration-Options plugin that moderates user registration (In BuddyPress. Currently the plugin sets the user status to 69 while the user is unapproved, and blocks access to BuddyPress functionality. However, the user is still able to login.

When they log-in, bbPress automatically sets the user forum role according to your setting in the back end. In this case it is set to 'spectator'. When the user is approved by the admin their status changes and I want it to also update the bbPress role to 'participant'.

Here is my first attempt:

function bp_registration_options_additional() {
  if ( is_user_logged_in() ) {  
    $user_ID = get_current_user_id();
    $user = get_userdata( $user_ID );
      if (69 !== $user->user_status ) {

      // Here is where I need help. 

     //How to set the bbPress forum role to 'Participant'? 

    }
  }
}
add_action( 'wp_loaded', 'bp_registration_options_additional' );

Thanks!

WP: 3.8 bbPress: Version 2.5.2

EDIT: A bit of additional information. The meta_key for the forum roles is: wp_capabilities. The meta_value of a user with the forum role 'spectator; is: a:2:{s:10:"subscriber";b:1;s:13:"bbp_spectator";b:1;}

1条回答
甜甜的少女心
2楼-- · 2019-05-27 12:04

Had the same requirement - to update the wp_capabilities field -

$wp_user_capabilities_arr = array( "subscriber" => true, "bbp_participant" => true ); update_user_meta($wp_user_id, "wp_capabilities", $wp_user_capabilities_arr);

And after update the data looks like:

a:2:{s:10:"subscriber";b:1;s:15:"bbp_participant";b:1;}

查看更多
登录 后发表回答