Show different roles in BBPress based off user membership

Hello,
I have something I would like to accomplish. As of now, I have this setup.
1. Free Membership=Basic Member.
2. Paid Member=Premium Member.
3. Staff Membership:Editor, Author.
Now in BBPress, I would like to use Basic Member or Premium Member instead of participant. I already have Membership2Pro setup to work this way, now we just need to add it to BBPress. Is this going to be possible?

Thanks.

  • Adam Czajczyk

    Hello Alex,

    I hope you're well today and thank you for your question!

    I must admit that this time I'm not quite following you. Could you please explain this a bit more for me?

    For example, I'm not sure which names (like "Free Membership", "Basic Member" etc) indicate Membership 2 Pro memberships and which are custom bbPress or WordPress user roles.

    Also, could you please share an example case scenario on user "workflow" from signup until posting on forum? This would help me "catch up" a bit :slight_smile:

    Thank you and I'm sorry for inconvenience!

    Best regards,
    Adam

  • Alex Stine

    Hello,
    OK here is how it goes.
    1. People who sign up for free.
    • Get Basic Member WP role.
    2. People who sign up for my monthly or yearly memberships.
    • Get the Premium Member WP role.
    3. People who I add to the Staff membership.
    • Editor, Author WP roles.
    I would like to display the user roles for 1-2 and display Staff for 3, on BBPress for the roles. I would like to leave the Administrator/Keymaster role alone.

    It is kind of like the membership system here, accept I created roles for people that sign up to that specific membership.

    Thanks.

  • Adam Czajczyk

    Hello Alex,

    Thanks for explanation, that sounds clear now :slight_smile:

    Assuming that by "membership" you mean "Membership 2 Pro" memberships and you got that all setup as well as there are all the necessary WP roles created, it can be achieved with an additional piece of code.

    The code would be as follows:

    <?php
    
    add_action( 'ms_model_relationship_create_ms_relationship_before', 'ms_controller_member_assign_memberships_done_cb', 99, 4 );
    function ms_controller_member_assign_memberships_done_cb( $membership_id, $user_id, $gateway_id, $move_from_id ) {
    	$user = new WP_User( $user_id );
    	switch( $membership_id ){
    		case 1343:
    			$user->set_role( 'editor' );
    			break;
    
    		case 1345:
    			$user->set_role( 'author' );
    			break;
    
    		case 1348:
    			$user->set_role( 'administrator' );
    			break;
    	}
    }

    To use it on your site please create an empty file with a ".php" extension (name doesn't matter here as long as it's unique) and paste this code inside, then upload it to your install's "/wp-content/mu-plugins/" folder. If there's no "mu-plugins" folder inside "wp-content", just create it please.

    You will also want to adjust the code a bit. The number right next to the "case" word is an ID of membership and for this membership you define WP-role by putting its name inside set_role() function.

    As a result, those who sign up for memberships used in this code will automatically be assigned defined WP-role.

    This would take care of points 1 and 2.

    As for point 3. As you're going to add members manually the easiest solution here will be to also assign them the WP role manually as well.

    I hope that helps!

    Best regards,
    Adam

  • Alex Stine

    Hello,
    This code does not look like the solution I needed.

    Let me try to explain this again.
    When a user signs up for free, they get the user role subscriber. The display name for subscriber is Basic Member. I would like to display Basic Member on BBPress instead of participant.

    When a user signs up for a yearly or monthly membership, they get the role premium member. This is a custom role I created. Instead of participant, It would like to display Premium Member.

    So the code needs to first identify what membership this person is apart of, then fetch the required role that I setup in Membership2Pro, then display it on BBPress instead of the default role.

    Does this make since?
    This topic might help explain: https://premium.wpmudev.org/forums/topic/adding-user-labels-that-show-current-user-role
    This is what I am trying to do accept in BBPress.

    Thanks.

  • Adam Czajczyk

    Hey Alex!

    Seems like I misunderstood you again. Basically, that's pretty much similar task I helped you solve in case of Support System plugin previously but this time it's for bbPress and Membership 2 Pro. I hope I get it now :slight_smile: Hopefully I'll be able to help you with this one to.

    Let me just break this into pieces and get confirmation.

    1. User signup
    2. While displaying bbPress forum we need to fetch users WP role and display its "display name" as a label next to user name in bbPress

    Would that be right?

    You differentiated "free" users and "members". However you're also saying that the User Role (WP-role) is assigned based on selected Membership. This would mean that Membership 2 Pro plugin is automatically giving members defined roles.

    Therefore, is that correct that in fact all I'd need to do here would be to check user-role and it's display name?

    An what about multiple roles? You can assign multiple roles to a single Membership with Membership 2 Pro Plugin. How would you like to handle this with bbPress?

    Please advise!

    Best regards,
    Adam

  • Alex Stine

    Hello,
    Membership2Pro automatically gives the user a role based on the membership they signed up for, so all you need to do is check the role say Premium member and then display it on BBPress instead of participant. or Basic Member instead of Participant. Basic Member is the display name of Subscriber, so Subscriber role has nothing to do with this project as far as I know. Premium Member is the role and display name. I just created it for a display name really, nothing special. It can do the same thing that the Subscriber role can theoretically.

    If you need to see for yourself, support access is open.
    Hope we got this somewhat more clear now :slight_smile:
    Thanks.

  • Adam Czajczyk

    Hello Alex!

    I hope you're well today!

    I'm sorry I wasn't able to respond to your earlier. As I'm working on weekends I need to "move my weekends" and use other days instead of Saturdays and Sundays. Mostly these are Mondays and Tuesdays (or Thursdays sometimes) instead.

    As for the code itself. I checked this and @Luís did pretty much most of the job here. The change here however would be a bit similar to the one we previously did for Support System. To sum it up, the steps would be as follow:

    1. I think you already have the "loop-single-reply.php" file inside "/bbpress" folder in your theme's folder. At this part let's follow suggestion that Luis shared:

    - comment or remove following line:

    <?php bbp_reply_author_link( array( 'sep' => '<br />', 'show_role' => true ) ); ?>

    2. Instead of that line use this code:

    <?php
    bbp_reply_author_link( array( 'sep' => '<br />', 'show_role' =>false ) );
    $reply_author_id = get_post_field( 'post_author', bbp_get_reply_id() );
    echo get_user_role( $reply_author_id );
    ?>

    3. Now place the following code into the "functions.php" file of your current theme:

    function get_user_role($user_id) {
    	global $wp_roles;
    	$roles = array();
    	$user = new WP_User( $user_id );
    	if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
    	foreach ( $user->roles as $role )
    		$roles[] .= translate_user_role($wp_roles->roles[$role]['name']);
    	}
    	return $roles[0];
    }

    This together should replace default roles shown by bbPress with those assigned by you manually and/or by Membership 2, using "display names" of the roles.

    It's worth noting though that in this case most users will have more than as single role assigned so the code shows the first one which should be the one you need. The second one would usually be the default bbPress role.

    I hope that helps!
    Best regards,
    Adam

  • Adam Czajczyk

    Hello Alex,

    Thanks for sharing the files with me. I talked to @Michael Bissett also but we decided that I'll carry this on since I'm involved in this from the beginning (including your previous similar post).

    That said, I've attached a .zip file to this post. The file includes both files that you shared with me.

    The functions.php file is the same file that you sent me but updated. It's to be used "as is" so just upload it to your site overwriting "functions.php" file of your current theme.

    There's also an updated "loop-single-reply.php" file.

    Would you please give it a try and let me know if it changed things a bit? Please make a backup of your current files though, just in case anything went wrong again.

    Best regards,
    Adam

  • Alex Stine

    Hello,
    Here is what the error log says.

    : bbp_setup_current_user was called incorrectly. The current user is being initialized without using $wp->init(). Please see Debugging in WordPress for more information. (This message was added in version 2.3.) in /site/ytadvise/public_html/wp-includes/functions.php on line 3792

    Warning: Cannot modify header information - headers already sent by (output started at /site/ytadvise/public_html/wp-includes/functions.php:3792) in /home/ytadvise/public_html/wp-content/plugins/sg-cachepress/class-sg-cachepress.php on line 335

    Notice: The called constructor method for WP_Widget is deprecated since version 4.3.0! Use
    __construct()
    instead. in /site/ytadvise/public_html/wp-includes/functions.php on line 3624

    Deprecated: Non-static method PHPPC::register_admin_menu() should not be called statically in /site/ytadvise/public_html/wp-includes/plugin.php on line 525
    Skip to main content Skip to toolbar

    Thanks.

  • Adam Czajczyk

    Hello Alex,

    Thanks for your feedback. Unfortunately, these notices doesn't seem to be related to the code we're working on here. I've tested this on my setup and it's not breaking it and I think I'm getting results that you expected.

    I understand that it's not that its not returning expected results but it's just causing site to break, is that correct? If so, I admit I'm a bit out of ideas here - in terms of further debugging. Would you mind me taking a closer look at your setup? I'd be great if I could access your site's dashboard and your install directly via FTP and check what may be going wrong here.

    If this would be possible, please send in:

    Subject: "Attn: Adam Czajczyk"

    - your site's WordPress admin login URL
    - admin login and password (may be a temporary account)
    - FTP credentials (hostname, username, password, port number if needed)
    - any other relevant URLs and information
    - link back to this thread

    Please use our contact form here https://premium.wpmudev.org/contact/.
    Select "I have a different question" from the drop-down list.

    Please be sure to make a full backup of your entire site first!

    Best regards,
    Adam

  • Adam Czajczyk

    Hello Alex!

    Would you please check your forum now? I think that's working but it would be great if you could just make sure that I didn't miss anything.

    I can see now that on a "Test" forum the roles displayed are:

    - "Administrator" for user "Alex S"
    - "Basic Member" for user "test test"

    Below is a brief summary of what I did:

    1. I checked the "functions.php" file of your current theme but that was fine so no changes there.
    2. I checked your theme and made following changes

    - the "bbpress" folder must be INSIDE your current theme folder so I moved it from "/theme" folder to "/theme/wpex-thoughts" folder

    - I removed existing "loop-single-replay.php" file from "bbpress" folder
    - I copied default "loop-single-replay.php" file from bbPress plugin to "bbpress" folder
    - I changed this line (around line number 43):

    <?php bbp_reply_author_link( array( 'sep' => '<br />', 'show_role' => true ) ); ?>

    to this code:

    <?php
    
    			bbp_reply_author_link( array( 'sep' => '<br />', 'show_role' => false ) );
    			$reply_author_id = get_post_field( 'post_author', bbp_get_reply_id() );
    			echo '<div class="bbp-author-role">' . get_user_role_redux( $reply_author_id ) . '</div>';
    ?>

    To sum it up:

    All those changes were basically the same as we talked about before, however the it needed a bit of "clean up". The essential issues here were:

    1. The "bbpress" folder containing customized bbPress templates must be located inside site's current theme folder
    2. the customized bbPress template cannot contain changes only - that's why I copied original template file and then changed it instead of putting only our customization there.

    I hope that helps!

    Best regards,
    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.