Skip to:
Content
Pages
Categories
Search
Top
Bottom

Show content for specific BP Groups only


  • Tunn
    Participant

    @iuriem

    I need to show a content only to a specific BP Group. I want a shortcode like this: [group “Group1”] Content [/group], as described @doremdou here, but she doesn’t share his solution. Can someone help with this?

    UPDATE

    Can this be used?

    add_shortcode( 'group', 'group_check_shortcode' );
    
    function group_check_shortcode( $atts, $content = null ) {
    	if( groups_is_user_member( bp_loggedin_user_id(), BP_Groups_Group::get_id_from_slug( 'group_slug_here' ))){
    		return do_shortcode($content);
    	}
    	return '';
    }
Viewing 4 replies - 1 through 4 (of 4 total)

  • Tunn
    Participant

    @iuriem

    I did it:

    add_shortcode( 'group', 'group_check_shortcode' );
    // usage: [group slug = 'group_slug_here'] content [/group]
    
    function group_check_shortcode( $atts, $content = null ) {
    
       extract(shortcode_atts(array( 'slug' => '', ), $atts));
    
    	if( groups_is_user_member( bp_loggedin_user_id(), BP_Groups_Group::get_id_from_slug( $slug ))){
    		return do_shortcode($content);
    	}
    
    	return 'Restricted content!';
    }

    OR

    add_shortcode( 'group', 'group_check_shortcode' );
    // usage: [group slug = 'group_slug_here'] content [/group]
    
    function group_check_shortcode( $atts, $content = null ) {
    
       extract(shortcode_atts(array( 'slug' => '', ), $atts));
    
    	if( groups_is_user_member( bp_loggedin_user_id(), groups_get_id( $slug ))){
    		return do_shortcode($content);
    	}
    
    	return 'Restricted content!';
    }

    Tunn
    Participant

    @iuriem


    pathuri
    Participant

    @pathuri

    I did insert this into functions.php.
    When I used the short code – only text message ‘content’ appears.
    Anything wrong from my side?


    Tunn
    Participant

    @iuriem

    This is what I use now. Try it:

    /**
     * Show content only to group members
     * Author: Iurie Malai
     */
    
    add_shortcode( 'group', 'group_check_shortcode' );
    // usage: [group id = YOUR_GROUP_ID_HERE] content [/group]
    
    function group_check_shortcode( $atts, $content = null ) {
    	extract(shortcode_atts(array( 'id' => '', ), $atts));
    	if( groups_is_user_member( bp_loggedin_user_id(), $id ) ) { return do_shortcode( $content ); }
    	if( $id == 1 ) {
    		return '<p>This will be seen only by logged in members of group with id = 1. In my case this is the group with all members of my site</p>';
    	} else { 
    		$group = groups_get_group( array( 'group_id' => $id ) );
    		//Put your User Groups Page Slug in the next code row.
    		//Usually this is "groups", but in my case it is different.
    		$url = home_url( $path = 'USER_GROUPS_PAGE_SLUG_HERE/' . $group->slug . '/', $scheme = relative );
    		return '<p>This will be seen only by logged in members of group with id that you specified by shortcode in your page or post. This will show the group name: <a href="' . $url . '">' .  $group->name . '</a>.</p>' ; }
    } /*---------------------------------------------------------*/
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Show content for specific BP Groups only’ is closed to new replies.
Skip to toolbar