Register link
-
I am trying to get all my links on my primary menu bar.
I have log in and log out links already on the primary menu by adding
add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 ); function add_loginout_link( $items, $args ) { if (is_user_logged_in() && $args->theme_location == 'primary-menu') { $items .= '<li><a href="'. wp_logout_url() .'">Log Out</a></li>'; } elseif (!is_user_logged_in() && $args->theme_location == 'primary-menu') { $items .= '<li><a href="'. site_url('wp-login.php') .'">Log In</a></li>'; } return $items; }to the functions file in my site.
What I am wondering is there a way for me to add the register link as well but have it disappear when they log in?
-
Try this (I’ve modified/enhanced your code a little):
add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 ); function add_loginout_link( $items, $args ) { if($args->theme_location == 'primary-menu') { if(is_user_logged_in()) { $items .= '<li><a href="'. wp_logout_url() .'">Log Out</a></li>'; } else { $items .= '<li><a href="'. site_url('wp-login.php') .'">Log In</a></li>'; $items .= '<li><a href="'. site_url('wp-login.php?action=register') .'">Register</a></li>'; } } return $items; }I added the code as follows
add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 ); function add_loginout_link( $items, $args ) { if($args->theme_location == 'primary-menu') { if(is_user_logged_in()) { $items .= '<li><a href="'. wp_logout_url() .'">Log Out</a></li>'; } else { $items .= '<li><a href="'. site_url('wp-login.php') .'">Log In</a></li>'; $items .= '<li><a href="'. site_url('wp-login.php?action=register') .'">Register</a></li>'; } } return $items; }it has made the login / Logout Link disappear and all that is shown is the Register link.
Weird?!
Let’s go back to your original code, then:
add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 ); function add_loginout_link( $items, $args ) { if (is_user_logged_in() && $args->theme_location == 'primary-menu') { $items .= '<li><a href="'. wp_logout_url() .'">Log Out</a></li>'; } elseif (!is_user_logged_in() && $args->theme_location == 'primary-menu') { $items .= '<li><a href="'. site_url('wp-login.php') .'">Log In</a></li>'; $items .= '<li><a href="'. site_url('wp-login.php?action=register') .'">Register</a></li>'; } return $items; }ok so this particular code is working for the login and logout
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; }how would i modify this to show register if the user is logged out and not if the user is logged in.
I made a boo boo and posted code from an old site originally I apologize for the confusion
Hmm… I don’t like the
ob_start()method…One sec…
Okay, try this one:
add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 ); function add_loginout_link( $items, $args ) { if($args->theme_location == 'primary') { if (is_user_logged_in()) { $items .= '<li><a href="'. wp_logout_url() .'">Log Out</a></li>'; } else { $items .= '<li><a href="'. site_url('wp-login.php') .'">Log In</a></li>'; $items .= '<li><a href="'. site_url('wp-login.php?action=register') .'">Register</a></li>'; } } return $items; }The problem was the
primary-menushould be justprimary.Absolutely Beautiful!!! That worked!!! You my friend are awesome 🙂 thanks for the quick help!!!
Lol… you’re very welcome 🙂
Have fun!!
The topic ‘Register link’ is closed to new replies.