BuddyDev

Search

[Resolved] buddypress member type as a body class name when user is logged in

  • Participant
    Level: Initiated
    Posts: 11
    waseem on #36646

    I have added this code but not worked for me

    /**
    * Inject classes to body based on displayed user’s member types.
    *
    * @param array $classes array of css classes.
    *
    * @return array
    */
    function buddydev_add_membertype_to_body_class( $classes = array() ) {

    if ( ! bp_is_user() ) {
    return $classes;
    }

    $member_types = bp_get_member_type( bp_displayed_user_id(), false );

    if ( ! $member_types ) {
    return $classes;
    }

    foreach ( $member_types as $member_type ) {
    $classes[] = ‘member-type-‘ . $member_type; // member-type-student etc.
    }

    return $classes;
    }

    add_filter( ‘bp_get_the_body_class’, ‘buddydev_add_membertype_to_body_class’ );

    I am using
    wordpress 5.6.2
    buddypress 7.2.0

  • Keymaster
    (BuddyDev Team)
    Posts: 24599
    Brajesh Singh on #36659

    Hi,
    That code is for displayed user’s member type.

    you may use the following instead

    
    
    /**
     * Inject classes to body based on logged user's member types.
     *
     * @param array $classes array of css classes.
     *
     * @return array
     */
    function buddydev_add_membertype_to_body_class( $classes = array() ) {
    
    	if ( ! is_user_logged_in() ) {
    		return $classes;
    	}
    
    	$member_types = bp_get_member_type( get_current_user_id(), false );
    
    	if ( ! $member_types ) {
    		return $classes;
    	}
    
    	foreach ( $member_types as $member_type ) {
    		$classes[] = 'logged-member-type-' . $member_type; // member-type-student etc.
    	}
    
    	return $classes;
    }
    
    add_filter( 'bp_get_the_body_class', 'buddydev_add_membertype_to_body_class' );
    
    

    This adds logged user’s member types.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 11
    waseem on #36673

    Thank you very much Brajesh,
    your code give me exactly what I need.

  • Keymaster
    (BuddyDev Team)
    Posts: 24599
    Brajesh Singh on #36691

    Hi Waseem,
    Thank you for confirming. I am glad it worked.

    Regards
    Brajesh

The topic ‘ [Resolved] buddypress member type as a body class name when user is logged in’ is closed to new replies.

This topic is: resolved