Skip to:
Content
Pages
Categories
Search
Top
Bottom

Display new tabs in selected groups only (group extension API)


  • maelga
    Participant

    @maelga

    Hi,

    I have created a tab in groups that redirects to members directory, using this code in function.php:

    if ( class_exists( 'BP_Group_Extension' ) ) :
    	class members_nav_tab_in_group extends BP_Group_Extension {
    			function __construct() {
    				$args = array(
    					'slug' => '../../members',
    					'name' => 'Members directory',
    					'nav_item_position' => 105,
    					);
    				parent::init( $args );
    			}
    		}
    	
    		bp_register_group_extension( 'members_nav_tab_in_group' );
    		
    	endif;

    It works well, but I now need this tab to appear only in 1 group and not all.

    The access or show_tab arguments don’t seem to serve my objective, so I have tried to restrict my code by adding:

    global $bp;
    $group_id = $bp->groups->current_group->id;
    if ( $group_id == '2' ) :
    

    But then the tab disappears in all groups.
    Any idea how to display this tab only in selected groups?

Viewing 5 replies - 1 through 5 (of 5 total)

  • shanebp
    Moderator

    @shanebp

    May not be the best way, but try:

    function add_group_tab() {
    
    	if ( class_exists( 'BP_Group_Extension' ) ) :
    	
    		class members_nav_tab_in_group extends BP_Group_Extension {
    			
    			function __construct() {
    			
    				if( bp_is_group_single() ) {
    					
    					$bp = buddypress();
    					
    					$group_id = $bp->groups->current_group->id;
    					
    					if( $group_id == 1 ) {
    					
    						$args = array(
    						'slug'              => '../../members',
    						'name'              => 'Members Directory',
    						'nav_item_position' => 105,
    						);
    						
    						parent::init( $args );
    					}
    				}
    			}
    		}
    	
    	bp_register_group_extension( 'members_nav_tab_in_group' );
    	
    	endif;
    
    }
    add_action( 'bp_loaded', 'add_group_tab' );

    maelga
    Participant

    @maelga

    Thanks @shanebp.
    It only works if I remove the function triggered at bp_loaded, as follows:

    	if ( class_exists( 'BP_Group_Extension' ) ) :
    		class members_nav_tab_in_group extends BP_Group_Extension {
    			function __construct() {
    				if( bp_is_group_single() ) {
    					$bp = buddypress();
    					$group_id = $bp->groups->current_group->id;
    					if( $group_id == '1' ) {
    						$args = array(
    						'slug'              => '../../members',
    						'name'              => 'Members Directory',
    						'nav_item_position' => 105,
    						);
    						parent::init( $args );
    					}
    				}
    			}
    		}
    	bp_register_group_extension( 'members_nav_tab_in_group' );
    	endif;
    

    What is the rationale/advantage for adding this function add_group_tab()?


    shanebp
    Moderator

    @shanebp

    I didn’t know where you were loading the code.
    Using the bp_loaded hook allows you to run it from bp-custom.php.


    maelga
    Participant

    @maelga

    From function.php

    Thanks again @shanebp


    maelga
    Participant

    @maelga

    If I were to create 2 or more tabs, what would be the best way to achieve this?
    I’ve been playing with the above code for some time without much success…

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display new tabs in selected groups only (group extension API)’ is closed to new replies.
Skip to toolbar