Skip to content

Instantly share code, notes, and snippets.

@ChromeOrange
Created September 2, 2012 10:20
Show Gist options
  • Save ChromeOrange/3596494 to your computer and use it in GitHub Desktop.
Save ChromeOrange/3596494 to your computer and use it in GitHub Desktop.
Add login / logout links to any menu and position it
<?php
/**
* Add login / logout links to any menu and position it
* Based On : http://www.viper007bond.com/2011/09/20/code-snippet-add-a-link-to-latest-post-to-wordpress-nav-menu/
*
* Instructions :
* 1 - Add this code to your theme functions file
* 2 - create a new custom menu item : http://cl.ly/image/092k303O3C2z DO NOT change this #loginlogout
* 3 - Position the menu item where ever you would like it to appear : http://cl.ly/image/0p00131v1L09
**/
// Front end only, don't hack on the settings page
if ( ! is_admin() ) {
// Hook in early to modify the menu
// This is before the CSS "selected" classes are calculated
add_filter( 'wp_get_nav_menu_items', 'replace_placeholder_nav_menu_item_with_latest_product', 10, 3 );
}
// Replaces a custom URL placeholder with the URL to the latest product
function replace_placeholder_nav_menu_item_with_latest_product( $items, $menu, $args ) {
// Loop through the menu items looking for placeholder(s)
foreach ( $items as $item ) {
// Is this the placeholder we're looking for?
if ( '#loginlogout' != $item->url )
continue;
if ( is_user_logged_in() ):
$outputurl = wp_logout_url( home_url() );
$outputtitle = 'Logout';
else :
$outputurl = get_permalink( woocommerce_get_page_id('myaccount') );
$outputtitle = 'Login';
endif;
// Replace the placeholder with the real URL / Title
$item->url = $outputurl;
$item->title = $outputtitle;
}
// Return the modified (or maybe unmodified) menu items array
return $items;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment