WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] custom login/logout item to menu (9 posts)

  1. halben
    Member
    Posted 7 months ago #

    Wondering if anyone can help me on this:

    I have the following:

    /*
     * -------------------------------
     * custom_login_out_item_to_menu()
     * -------------------------------
     */
    
    add_filter('wp_nav_menu_items', 'custom_login_out_item_to_menu', 50, 2);
    function custom_login_out_item_to_menu($items, $args) {
    	if (is_admin() || $args -> theme_location != 'top')
    		return $items;
    
    	$redirect = ( is_home()) ? false : get_permalink();
    
    	if (is_user_logged_in())
    		$link = '<a href="' . wp_logout_url($redirect) . '" title="' . __('Logout') . '">' . __('Logout') . '</a>';
    	else
    		$link = '<a href="' . wp_login_url($redirect) . '" title="' . __('Login') . '">' . __('Login') . '</a>';
    
    	return $items .= '<li id="log-in-out-link" class="menu-item menu-type-link">' . $link . '</li>';
    }

    I'm trying to add a custom login / logout link through my child theme functions.php. When I paste this into my functions.php, it's suppose to generate a login/logout link in my theme location : Top Navigation.

    The problem is, it's now showing anything. Can anyone please help fix this?

    Thanks,
    Hal

  2. halben
    Member
    Posted 7 months ago #

    Correction:

    The problem is, it's "not" showing anything. Can anyone please help fix this?

  3. esmi
    Theme Diva & Forum Moderator
    Posted 7 months ago #

    $redirect
    (string) (optional) URL to redirect to on login. Must be absolute (as in, http://example.com/mypage/). Recommended: site_url( '/mypage/ ' ).

    http://codex.wordpress.org/Function_Reference/wp_login_url

    I not sure that get_permalink() is going to work here.

  4. halben
    Member
    Posted 7 months ago #

    Would that cause the links not to be displayed?

  5. esmi
    Theme Diva & Forum Moderator
    Posted 7 months ago #

    It might do. I've not used either of those functions in this way myself. Try replacing:

    `$redirect = ( is_home()) ? false : get_permalink();

    with:

    $redirect = 'http://your_domain/;

    as a test.

  6. halben
    Member
    Posted 7 months ago #

    Besides that, I tried commenting out $redirect and ran a test. Nothing shows up. Could it be that it's not reading the right theme location?

    theme location : Top Navigation should be 'top' I assume?

  7. esmi
    Theme Diva & Forum Moderator
    Posted 7 months ago #

    theme location : Top Navigation should be 'top' I assume?

    Not necessarily. You need to check in the theme's functions.php file to determine the actual id of that top menu.

  8. halben
    Member
    Posted 7 months ago #

    I'll look into it and get back to you.

  9. halben
    Member
    Posted 7 months ago #

    That did the trick.

    Thanks,
    esmi

Reply

You must log in to post.

About this Topic