Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using the code below to add a Login/Logout menu item to WordPress programatically. I am trying to figure out how to specify the placement of this menu to be in the #2 position.

IE - Main Menu

Home | Login/Logout | Contact Us

add_filter('wp_nav_menu_items', 'voodoo_loginout_link', 10, 2); 

function voodoo_loginout_link($items, $args) {
    $voodooredir = htmlspecialchars($_SERVER['REQUEST_URI']);
    $loginoutlink = wp_loginout($voodooredir, false);
    return $items . "<li> $loginoutlink </li>";
}
share|improve this question
    
In addition, can we change the default text of Login or Logout to "Dealer Login" or "Dealer Logout". –  user45670 Feb 3 at 0:07
1  
What menu? How is this function called? Presumably hooked into something somewhere? –  s_ha_dum Feb 3 at 0:23
    
add_filter('wp_nav_menu_items', 'voodoo_loginout_link', 10, 2); –  user45670 Feb 3 at 0:31
    
There isn't what I'd consider a good way to do this. Have you considered using something like wp_create_nav_menu? –  s_ha_dum Feb 3 at 15:27
add comment

1 Answer

I am going to use this workaround. Add a menu link with wp-login.php and replace the Title/URL.

if ( !class_exists( 'HijackMe' ) ) {
class HijackMe {
    public function hijack_menu($objects) {
        /**
     * If user isn't logged in, we return the link as normal
     */
    if ( !is_user_logged_in() ) {
        return $objects;
    }
    /**
     * If they are logged in, we search through the objects for items with the 
     * class wl-login-pop and we change the text and url into a logout link
     */
    foreach ( $objects as $k=>$object ) {
        if ( in_array( 'wl-login-pop', $object->classes ) ) {
            $objects[$k]->title = 'Logout';
            $objects[$k]->url = wp_logout_url();
            $remove_key = array_search( 'wl-login-pop', $object->classes );
            unset($objects[$k]->classes[$remove_key]);
        }
    }

    return $objects;
}
}

}

$hijackme = new HijackMe;

add_filter('wp_nav_menu_objects', array($hijackme, 'hijack_menu'), 10, 2);

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.