WordPress.org

Ready to get started?Download WordPress

Forums

Showing/Changing Menu Items with Login/Logout (9 posts)

  1. Recker
    Member
    Posted 6 months ago #

    Hey I've been using this code which has been working great for pulling out a modal login window from my site bar: http://www.Protipsters.co.uk

    function add_login_out_item_to_menu( $items, $args ){
    
    	//change theme location with your them location name
    	if( is_admin() ||  $args->theme_location != 'primary' )
    		return $items; 
    
    	$redirect = ( is_home() ) ? false : get_permalink();
    	if( is_user_logged_in( ) )
    		$link = do_shortcode('[wp-modal-login login_text="inloggen" logout_text="Uitloggen" logout_url="/"]');
    	else  $link = do_shortcode('[wp-modal-login login_text="inloggen" logout_text="Uitloggen" logout_url="/"]');
    
    	return $items.= '<li id="log-in-out-link" class="menu-item menu-type-link">'. $link . '</li>';
    }add_filter( 'wp_nav_menu_items', 'add_login_out_item_to_menu', 50, 2 );

    But I'm now trying to get a second auto-changing menu item which instead of calling for a shortcode simply redirects to another page.
    For example:

    If user is not logged in:
    Menu bar shows - 'Subscribe Now' = Sends user to subscription Page

    If user is logged in:
    Menu bar shows - 'Account' = Sends user to account page

    I've tried changing and editing the code, but it keeps causing horrendous errors and I keep seem to get it working.
    Any help with editing the code would be great!

    Thanks in advance (:
    - Jamie

  2. bcworkz
    Member
    Posted 6 months ago #

    Have you tried replacing the is_user_logged_in logic of the above code with something like this?

    if( is_user_logged_in( ) )
        $link = '<a href="account-page.htm" class="login">Account</a>';
    else
        $link = '<a href="subsciption-page.htm" class="login">Subscribe Now</a>';

    Adjust the hrefs to the actual page URLs of course.

  3. Recker
    Member
    Posted 6 months ago #

    I tried that, It just showed a black screen! Didn't load any of the site.
    Though it didn't show any error code either, So It must be in the right direction!

  4. bcworkz
    Member
    Posted 6 months ago #

    Have you checked for javascript errors? The modal script may be crashing because it's elements have become malformed. It needs to be kept happy or removed, depending on which way you're going with this.

  5. Recker
    Member
    Posted 6 months ago #

    Where would a javascript error log be found?
    And I need the modal login/logout connected to the main menu. And it would be useful to have this to!

    Since I'm basically repeating code with a different layout, I don't need this part again do i?

    function add_login_out_item_to_menu( $items, $args ){
    
    	//change theme location with your them location name
    	if( is_admin() ||  $args->theme_location != 'primary' )
    		return $items;

    Thanks for the help!
    - Jamie

  6. bcworkz
    Member
    Posted 6 months ago #

    Where Javascript errors show up is browser dependent. On FF it's "Error Console" under "Web Developer".

    Yes you only need that snippet once, it suppresses anything that follows under certain conditions.

    If the modal script works the way I think it does, the added menu items should work if you assign the same class. But it's conceivable the script may need some tweaking, especially considering that now nothing appears to be working. Hopefully there are clues in the error console, otherwise I'm out of ideas.

  7. Recker
    Member
    Posted 6 months ago #

    I've managed to get it partly working!: protipsters.co.uk

    This is the code I'm using:

    /*Add Modal Menu Login/Logout*/
    function add_login_out_item_to_menu( $items, $args ){
    
    	//change theme location with your theme location name
    	if( is_admin() ||  $args->theme_location != 'primary' )
    		return $items; 
    
    		if( is_user_logged_in( ) )
        $link = '<a href="protipsters.co.uk/account" class="login">Account</a>';
    else
        $link = '<a href="protipsters.co.uk/subscription" class="login">Subscribe Now</a>';
    
    	$redirect = ( is_home() ) ? false : get_permalink();
    	if( is_user_logged_in( ) )
    		$link = do_shortcode('[modal_login login_text="Members Login" logout_text="Logout" logout_url="/"]');
    	else  $link = do_shortcode('[modal_login login_text="Members Login" logout_text="Logout" logout_url="/"]');
    
    	return $items.= '<li id="log-in-out-link" class="menu-item menu-type-link">'. $link . '</li>';
    }add_filter( 'wp_nav_menu_items', 'add_login_out_item_to_menu', 50, 2 );

    The issue I'm having now is that the Subscribe Now/Account button is stuck on Account all the time. It doesn't change name, but it does appear to do what it's meant to do!

  8. Recker
    Member
    Posted 6 months ago #

    No, i'm wrong again! The cache hadn't cleared and it was still showing an old link to account via role redirect.

    Still haven't managed to get it into a menu!

  9. bcworkz
    Member
    Posted 6 months ago #

    Well, I see one problem. The subsequent $link = do_shortcode(\\... lines are overwriting your $link assignments so it's like they were never there. You need to concatenate additional content to $items after each step. Add this above the $redirect = ( is_home()\\... line:
    $items .= $link;

    This may not be the only problem, but it's certainly a big problem.

Reply

You must log in to post.

About this Topic