September 12, 2017 at 9:15 am
Hello
FYI: I’m using a child theme for the Virtue theme and have added a functions.php file to the child folder.
I added a menu option to the menu section of the theme (Appearance/Menu; I have a screen capture if you need it) to the Top Menu menu called “My Account” (I’m using WooCommerce). Oddly enough – the “menu icon” only shows visible when the user is actually logged in to my site and not all the time as I hoped for.
My goal was to have this menu icon visible at all times. This way if the user was not logged in, and later clicked the menu icon, s/he would be forwarded to the My Account page and would be required to log in since the session variable wasn’t active. If, on the other hand, the user was in fact logged in and clicked this icon while logged in, the user would “reload” [re-directed] the page and still able to see “their” settings, purchases, etc.
Since this didn’t happen, I added the following to my child functions.php page:
add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link($items, $args) {
$pageURL = 'https://';
$pageURL .= $_SERVER['HTTP_HOST'];
$pageURL .= $_SERVER['REQUEST_URI'];
if( !($args->theme_location == 'topbar_navigation') )
return $items;
global $pinnacle;
ob_start();
wp_loginout($pageURL);
$loginoutlink = ob_get_contents();
ob_end_clean();
$items .= '<li>'. $loginoutlink .'</li>';
return $items;
}
This now shows the words “Log In” on the top menu when not logged in, and “Log Out” when the user is logged in. But this has presented two new issues:
1. When the user is not logged in and later clicks it, it’s redirecting the user to the WordPress login page and not the My Account (my-account) page.
2. No icon associated with it (its just words; kind of boring).
I’d like to use the approach of the icon showing all the time without the adding the code to the functions page and if the user isn’t logged in – make them log in (redirecting to the My Account); or if the user is logged in and its clicked again the user is redirected back to the same place (My Account page).
So the question is…
Can something be changed (like a setting that I missed) to allow my first approach to work? If not, is there a code change to the above that will satisfy the redirecting the user to the My Account page and at the same time – that will add an icon to the Top Menu?
TIA –
Jim