• Hi, I’ve been reading up on the wp_register function and it sounds like just what I need to display a Register button only when the user is not logged in.

    But I can’t figure out how to use it to place the link in the main nav menu. Please can anyone help?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Have you checked the Anyone Can Register box in the Settings–>General menu in your admin dashboard?

    Moderator bcworkz

    (@bcworkz)

    You can use the “wp_nav_menu” or “wp_nav_menu_items” filters if your theme is using wp_nav_menu() to output the menus (most themes do). Unless you want the link added to all menus, you need to collect the second $args parameter passed to determine which menu is being output and conditionally add the link only to the desired menu.

    Alternately, determine the menu slug you want to target and use the “wp_nav_menu_{$menu->slug}_items” dynamic filter to only add the link to the specific menu.

    Note that the wp_register() function returns a log in/out link. By default it is wrapped in <li> tags which means the link will likely be styled as an additional menu item, not an actual “button”. If you really want it to look like a button that appears immediately after the menu, you’ll need some custom CSS, which can be added in the Customizer.

    • This reply was modified 6 years, 7 months ago by bcworkz. Reason: fixed li html
    Thread Starter Hungrynow

    (@hungrynow)

    Thank you for replying!
    I admit I’m a little confused now. I have already added a login/logout link to my menu with this code in my functions.php:

    add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
    function add_login_logout_link($items, $args) {
            ob_start();
            wp_loginout('index.php');
            $loginoutlink = ob_get_contents();
            ob_end_clean();
            $items .= '<li>'. $loginoutlink .'</li>';
        return $items;
    }
    

    What I would like now is an additional menu item to register that shows when the login link shows. (Button was the wrong terminology, sorry, I just want a standard menu item). So how can I do this?

    Moderator bcworkz

    (@bcworkz)

    Sorry for any confusion, it would be the result of my own lack of attention and use of proper terminology. I can’t even claim it’s due to ambiguous terminology like “button”, my terminology was just plain wrong. Let’s try again.

    You add the link just like you did for log in/out. Use is_user_logged_in() to conditionally add the generated registration link. Of course this would result in the logged in admin link never being displayed, which apparently is exactly what you are after. You can even use the log in/out callback function, conditionally calling wp_register().

    Incidentally, both the register and log in/out functions accept an optional “echo” parameter. When false is passed, the function returns the resulting HTML instead of echoing it out. Thus you don’t need to buffer the output, just do this:
    $items .= '<li>'. wp_loginout('index.php', false ) .'</li>';

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to add Register button to main menu’ is closed to new replies.