This is searchable archive of our old support forums, which operated from 2012 - 2016. To find out how to get support for your current theme, please visit our support page.

Errors in setting up separate menus for logged in/logged out members

  • Creator
    Topic
  • #11246
    Joseph Sellers
    Participant

    Hi,

    I am trying to work out how to have a different menu for logged out users and logged in users.

    This is the functions.php that I have in the child theme.

    http://pastebin.com/Semp4zs1

    This works, nearly but it creates problems whenever a page is saved and users are facing issues when logging in.

    I checked the error log and I think this could be the error that is indicating the problem.

    PHP Warning: Cannot modify header information – headers already sent by (output started at /home/woodwork/public_html/wp-content/themes/jumpstart-stretch/functions.php:62) in /home/woodwork/public_html/wp-includes/pluggable.php on line 875

    Can someone please take a look and tell me what I am doing wrong?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #11259
    Jason Bobich
    Keymaster

    Hello,

    This function you’ve declared logged_inout_menu, you forgot to close it. In other words, you’re missing a } at the end, and maybe have confused your braces up earlier in the function.

    Here’s more like how you want to format that section:

    function logged_inout_menu() {
    	do_action( 'themeblvd_header_menu_before' );
    	?>
    	<a href="#access" class="btn-navbar">
    		<?php echo apply_filters( 'themeblvd_btn_navbar_text', '<i class="icon-reorder"></i>' ); ?>
    	</a>
    	<nav id="access" role="navigation">
    		<div class="access-inner">
    			<div class="access-content clearfix">
    	
    				<?php 
    				if( is_user_logged_in() ){
    					wp_nav_menu( apply_filters( 'themeblvd_primary_menu_args', array( 'menu_id' => 'primary-menu', 'menu_class' => 'sf-menu','container' => '', 'theme_location' => 'logged-in', 'fallback_cb' => 'themeblvd_primary_menu_fallback' ) ) );
    				} else {
    					wp_nav_menu( apply_filters( 'themeblvd_primary_menu_args', array( 'menu_id' => 'primary-menu', 'menu_class' => 'sf-menu','container' => '', 'theme_location' => 'logged-in', 'fallback_cb' => 'themeblvd_primary_menu_fallback' ) ) );
    				}
    				
    				themeblvd_header_menu_addon();
    				?>
    	
    			</div><!-- .access-content (end) -->
    		</div><!-- .access-inner (end) -->
    	</nav><!-- #access (end) -->
    
    	<?php
    	do_action( 'themeblvd_header_menu_after' );
    }
    remove_action( 'themeblvd_header_menu', 'themeblvd_header_menu_default' );
    add_action( 'themeblvd_header_menu', 'logged_inout_menu' );

    And notice how there’s a filter “themeblvd_primary_menu_args” in there? You could actually do this instead of the above, which might be a bit cleaner and not make you mess with around with other stuff that doesn’t correspond to what you’re editing (i.e the menu theme_location).

    function my_primary_menu_args( $args ) {
    
    	if( is_user_logged_in() ) {
    		$args['theme_location'] = 'logged-in';
    	} else {
    		$args['theme_location'] = 'logged-out';
    	}
    	
    	return $args;
    }
    add_filter('themeblvd_primary_menu_args', 'my_primary_menu_args');

    And where you’re adding your theme menu locations, this is another good example of where you should be utilizing the filter. Only use pluggable functions as a last resort. So, here’s how you could do essentially the same thing with a filter there as you’re accomplishing now with the pluggable function:

    function my_nav_menus( $menus ) {
    
    	$menus = array(
    		'logged-in'		=> __( 'Logged In Menu', 'themeblvd' ),
    		'logged-out'	=> __( 'Logged Out Menu', 'themeblvd' ),
    		'footer'		=> __( 'Footer Navigation', 'themeblvd' )
    	);
    
    	return $menus;
    
    }
    add_filter( 'themeblvd_nav_menus', 'my_nav_menus' );

    More about how filters work: http://dev.themeblvd.com/tutorial/filters/

    Also, here’s another big thing I’m noticing throughout your file, which is actually the direct cause for the error you originally cited. With PHP, you can’t start the output buffer early, before the HTML document is outputted on the page. You’re doing this accidentally with all the places in your functions.php where you’re opening and closing PHP outside of your functions.

    So, for example say you had this:

    function foo() {
    	// do something ...
    }
    
    ?>
    
    <?php
    
    function bar() {
    	// do something ...
    }

    Where PHP is being closed and opened again randomly in the middle, you’re actually outputting blank space there. This type of thing is what will give you a “Cannot modify header information” warning or error.

    #11312
    Joseph Sellers
    Participant

    That is fantastic. Thank you so much for your help.

    Best,

    Joseph

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.