Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wp-kitten/302ce4bc8e6a9b6f9155ebcdb4d24bc1 to your computer and use it in GitHub Desktop.
Save wp-kitten/302ce4bc8e6a9b6f9155ebcdb4d24bc1 to your computer and use it in GitHub Desktop.
Add custom link to menu to logged in users only
// Filter wp_nav_menu() to add additional links
function kallyasChildExtendMenuNav($items, $args = null)
{
// Make sure the arguments are passed correctly
if(! empty($args) && is_object($args))
{
// Make sure the WooCommerce plugin exists and is activated
if( class_exists('WooCommerce') )
{
// Make sure we're updating the correct menu: top nav
if(isset($args->menu) && isset($args->menu->slug) && $args->menu->slug == 'top-nav')
{
// Make sure the user is logged in
if( is_user_logged_in())
{
// Add the link to MyAccount page
$myaccount_page_id = get_option( 'woocommerce_myaccount_page_id' );
if ( $myaccount_page_id ) {
$myaccount_page_url = get_permalink( $myaccount_page_id );
// add the My Account link to the end of the menu
$items = $items . '<li><a href="'.$myaccount_page_url.'">'.__('My Account', 'zn_framework').'</a></li>';
}
}
}
}
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'kallyasChildExtendMenuNav', 2, 99 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment