bbPress tips & tricks

I recently had to use the bbPress plugin on one of my WordPress projects. Here are some helpful tips and tricks I wish I knew before using this WordPress plugin.

You need to enable registration

If you are allowing public user registration you need to enable the “Anyone can register” setting under General > Membership in the WordPress admin dashboard.

You should to create a register & login screen

You can technically use the default register & login screen that WordPress provides but it’s a much better user experience if you create your own pages because they will visually fit better within your theme.

You can simply use these two provided bbPress shortcodes on your own personalized register & login pages:

  • [bbp-login] – Display the login screen.
  • [bbp-register] – Display the register screen.

Once you’ve created your own pages you can add them to your menu(s) so the user can easily access them.

Customize the WordPress logo & link

It’s a good idea to replace the default WordPress logo & link on the default WordPress register and login screens if the user decides to use this instead of your own customized pages.

Here’s some helpful ways on how to do it on the WordPress codex.

You’ll want to change this to something that is more personalized.

Use a reCAPTCHA plugin on the register page

This will help to reduce spam and bot signups if you allow public user registration.

I recommend using the CAPTCHA 4WP plugin, this will add a google reCAPTCHA field to both bbPress shortcode registration (using [bbp-register]) and the default WordPress registration page.

Add a redirect for users after they login

This function will redirect users with the “subscriber” role to the bbPress forums instead to your homepage.

Place this function in your functions.php.

/**
 * Redirect user after successful login.
 *
 * @param string $redirect_to URL to redirect to.
 * @param string $request URL the user is coming from.
 * @param object $user Logged user's data.
 * @return string
 */

function my_login_redirect( $redirect_to, $request, $user ) {
	//is there a user to check?
	if ( isset( $user->roles ) && is_array( $user->roles ) ) {
		//check for admins
		if ( in_array( 'administrator', $user->roles ) ) {
			// redirect them to the default place
			return $redirect_to;
		}elseif( in_array( 'subscriber', $user->roles ) ){
			//redirect subscribers to bbpress forum
			return home_url('forums');
		} else {
			return home_url();
		}
	} else {
		return $redirect_to;
	}
}

add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

Link to the bbPress profile page in the admin bar

This function will change profile and edit profile links in admin bar to link to the bbPress profile page instead of the default WordPress one.

You’ll be changing these links in the admin bar

Place this function in your functions.php.

/**
 * Get user role
 */

function cmyee_get_user_role( $user_id = 0 ) {
    $user = ( $user_id ) ? get_userdata( $user_id ) : wp_get_current_user();
    return current( $user->roles );
}

/**
 * Change profile + edit profile links in admin bar
 */

add_action( 'admin_bar_menu', function( $wp_admin_bar ) {
    //get user role
    cmyee_get_user_role();

    //check if current user has subscriber role
    if( cmyee_get_user_role() == 'subscriber' ){
        $current_user = wp_get_current_user();
        $user = $current_user->user_nicename ;

        $profile_link = home_url('/community/users/') . $user;
        $edit_profile_link = $profile_link . '/edit';

        if ( $wp_admin_bar->get_node( 'my-account') ) {
            $wp_admin_bar->add_node( [
                'id'   => 'my-account',
                'href' => $profile_link,
            ] );
        }
        if ( $wp_admin_bar->get_node( 'user-info') ) {
            $wp_admin_bar->add_node( [
                'id'   => 'user-info',
                'href' => $profile_link,
            ] );
        }
        if ( $wp_admin_bar->get_node( 'edit-profile') ) {
            $wp_admin_bar->add_node( [
                'id'   => 'edit-profile',
                'href' => $edit_profile_link,
            ] );
        }
    }
} );

Leave a Reply

Your email address will not be published. Required fields are marked *