Skip to content

Instantly share code, notes, and snippets.

@dingo-d
Last active October 7, 2016 12:26
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 dingo-d/dfd2e16f9c2d2e885deb3c21bfb98b1c to your computer and use it in GitHub Desktop.
Save dingo-d/dfd2e16f9c2d2e885deb3c21bfb98b1c to your computer and use it in GitHub Desktop.
Add additional menu items to the existing menu. In this case it's adding My Account page with endpoints as submenus.
<?
// Check if the WooCommerce plugin is activated.
if (in_array('woocommerce/woocommerce.php', get_option('active_plugins')) ) {
add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
}
/**
* Add login/my account link
*
* Adding additional link to header menu. Be sure that your menu location is named
* header-menu, othervise this won't work.
*
* @param string $items Additional menu items to append.
* @param object $args Menu arguments object.
* @return string Menu items modified.
* @since 1.0.0
*/
function add_loginout_link( $items, $args ) {
if ( $args->theme_location == 'header-menu' ) {
if ( is_user_logged_in() ) {
// WooCommerce endpoints.
$orders = wc_get_endpoint_url( 'orders', '', get_permalink( woocommerce_get_page_id( 'myaccount' ) ) );
$edit_account = wc_get_endpoint_url( 'edit-account', '', get_permalink( woocommerce_get_page_id( 'myaccount' ) ) );
$addresses = wc_get_endpoint_url( 'edit-adresses', '', get_permalink( woocommerce_get_page_id( 'myaccount' ) ) );
$payment_methods = wc_get_endpoint_url( 'payment-methods', '', get_permalink( woocommerce_get_page_id( 'myaccount' ) ) );
$hours_spent = wc_get_endpoint_url( 'hours-spent', '', get_permalink( woocommerce_get_page_id( 'myaccount' ) ) );
// Build menu item with subitems.
$items .= '<li class="menu_iem_logout"><a href="' . get_permalink( woocommerce_get_page_id( 'myaccount' ) ) . '">' . esc_html__( 'My Account', 'mytheme' ) . '</a>
<ul class="submenu">
<li><a href="' . esc_url( $orders ) . '">' . esc_html__( 'Orders', 'mytheme' ) . '</a></li>
<li><a href="' . esc_url( $edit_account ) . '">' . esc_html__( 'Account Details', 'mytheme' ) . '</a></li>
<li><a href="' . esc_url( $addresses ) . '">' . esc_html__( 'Addresses', 'mytheme' ) . '</a></li>
<li><a href="' . esc_url( $payment_methods ) . '">' . esc_html__( 'Payment Methods', 'mytheme' ) . '</a></li>
<li><a href="' . esc_url( $hours_spent ) . '">' . esc_html__( 'Hours Spent', 'mytheme' ) . '</a></li>
<li><a href="' . esc_url( wp_logout_url( home_url() ) ) . '">' . esc_html__( 'Logout', 'mytheme' ) . '</a></li>
</ul>
</li>';
} else {
$items .= '<li class="menu_iem_login"><a href="' . get_permalink( woocommerce_get_page_id( 'myaccount' ) ) . '">' . esc_html__( 'Log In', 'mytheme' ) . '</a></li>';
}
}
return $items;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment