Created
May 27, 2016 16:22
-
-
Save wp-kitten/302ce4bc8e6a9b6f9155ebcdb4d24bc1 to your computer and use it in GitHub Desktop.
Add custom link to menu to logged in users only
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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