Get 15% off on all our plugins, themes and memberships till 4th December. Use coupon: BLACKFRIDAY2021.

BuddyDev

Search

Variable Outside Function

Tagged: ,

  • Participant
    Level: Initiated
    Posts: 16
    eGuard on #1805

    Hi Guys,

    I have a problem i cannot fix. I have searched everywhere but without any luck.
    I hope you Buddypress experts can help me on this

    I have below code to create a subnav tab function to create a subnav tab. I have set $collection term to the value i want. Below function works, no problem there! But what i want to do is to take the variable outside of the function, which doesnt seem to work.

    WORKING CODE:

    
    
    function my_custom_subnav() {
    	global $bp; 
    	$collection_term = 'subnav  tab name';
    	$collection_term_slug = preg_replace('/\s+/', '-', $collection_term); // replace spaces with hyphens.
    	  $collection_term_slug = preg_replace('/[^A-Za-z0-9\-]/', '', $collection_term); // Removes special chars.
    	$screen_function_name = 'subnav_function_to_show_' . $collection_term_slug;
    	
    
    	bp_core_new_subnav_item( array( 
    		'name' => $collection_term,  
    		'slug' => $collection_term_slug,  
    		'screen_function' => 'screen_function_name', 
                    'parent_url'		=> trailingslashit(  bp_displayed_user_domain()  . 'collections' ),
    		'parent_slug'		=> 'collections',
    		'position'			=> 100,
    	
    	) );
    	
    	
    			 }
    
    $bp->bp_subnav['TAXONOMY TERM'] = false;
    
    function screen_function_name() {
    
    	//add title and content here – last is to call the members plugin.php template
    	add_action( 'bp_template_content', 'subnav_function_to_show_screen_content' );
    	bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
    }
    
    function subnav_function_to_show_screen_content() {
    	ECHO 'posts in '. $collection_term ;
    }
    
    add_action( 'bp_setup_nav', 'my_custom_subnav' );
    
    

    WHAT I WANT TO ACHIEVE:

    
    $collection_term = 'subnav  tab name';  /* variable outside the function */
    
    function my_custom_subnav() {
    	global $bp; 
    	$collection_term_slug = preg_replace('/\s+/', '-', $collection_term); // replace spaces with hyphens.
    	  $collection_term_slug = preg_replace('/[^A-Za-z0-9\-]/', '', $collection_term); // Removes special chars.
    	$screen_function_name = 'subnav_function_to_show_' . $collection_term_slug;
    	
    
    	bp_core_new_subnav_item( array( 
    		'name' => $collection_term,  
    		'slug' => $collection_term_slug,  
    /* rest of the code */
    

    I have tried to set the $collection_term to Global, but that also doesnt seem to be working.
    Please help me out, I have aged a lot trying to solve this mystery!

    • This topic was modified 6 years ago by eGuard.
  • Keymaster
    (BuddyDev Team)
    Posts: 19833
    Brajesh Singh on #1810

    Hi,
    Have you tried this

    
    
    function my_custom_subnav() {
    	global $bp;
            global $collection_term;
    	$collection_term_slug = preg_replace('/\s+/', '-', $collection_term); // replace spaces with hyphens.
    	  $collection_term_slug = preg_replace('/[^A-Za-z0-9\-]/', '', $collection_term); // Removes special chars.
    	$screen_function_name = 'subnav_function_to_show_' . $collection_term_slug;
    	
    
    	bp_core_new_subnav_item( array( 
    		'name' => $collection_term,  
    		'slug' => $collection_term_slug,  
    /* rest of the code */
    
    

    and make sure that you initialize $collection_term before making a call to

    
    
    my_custom_subnav()
    
    

    That should work. Can you please give it a try. If you are initializing $collection_term in another function, then declare it global there too. Also, It must be initialized before call to the function.

  • Participant
    Level: Initiated
    Posts: 16
    eGuard on #1816

    Dear Brajesh,

    Sorry, I am not a developer so some terms i have to look up.
    Initializing a variable means giving it a value, right? Like $collection_term = ‘foo’;

    I am looking to get taxonomyterm values by user, and for each value create a subnav tab.
    In any case the first step to achieve that is to be able to use a variable from outside the function.

    I have placed below code in bp-custom.php, but without any result. I have also tried to use $GLOBALS[‘collection_term’], but also no result there.

     
    $collection_term = 'test';
    
    function my_custom_subnav() {
    	global $bp;
            global $collection_term;
    	
    	$collection_term_slug = strtolower($collection_term);//lowercase
            $collection_term_slug = preg_replace('/\s+/', '-', $collection_term); // replace spaces with hyphens.
    	$collection_term_slug = preg_replace('/[^A-Za-z0-9\-]/', '', $collection_term); // Removes special chars.
    	$screen_function_name = 'subnav_function_to_show_' . $collection_term_slug;
    	
    	bp_core_new_subnav_item( array( 
    		'name' => $collection_term,  
    		'slug' => $collection_term_slug,  
    		'screen_function' => 'screen_function_name', 
                    'parent_url'		=> trailingslashit(  bp_displayed_user_domain()  . 'collections' ),
    		'parent_slug'		=> 'collections',
    		'position'			=> 100,
    	
    	) );
    			 }
    

    I have hired some developers look at the code, and try to implement what i am looking to achieve, but they have been unsuccesful in their task. By the way, i read somewhere that global variables will be depreciated in php6.

    Your help is highly appreciated! Would it be possible to hire you to implement the code?

    • This reply was modified 6 years ago by eGuard.
  • Keymaster
    (BuddyDev Team)
    Posts: 19833
    Brajesh Singh on #1819

    Hi,
    1. Globals are not going to be deprecated in PHP 7 and there is little/no chance that it will ever happen.

    2. Since the code is not complete, I can not guess how you are taking the details from the user.

    I am sorry but I am not available for hire at the moment due to my commitments with BuddyDev work. But, Your task seems easy enough to me and you do not need to hire me to get it completed.

    Please send me the code on brajesh@buddydev.com and I will send you the updated one. This seems to be small functionality that should not take much time.

You must be logged in to reply to this topic.

This topic is: not resolved