Sarathlal N

Add new menu item in my account navigation - WooCommerce

In a recent WooCommerce project, my client suggest to add a new menu item in the WooCommerce user’s My Account navigation.

When we add a menu item, we have to also create a new page with some content for that new menu link.

Below you can find the simple code snippet to create a new menu item & page to add content for this new menu item.

/*
 * Step 1. Add new menu item to My Account menu - on the 3rd position.
 */
add_filter ( 'woocommerce_account_menu_items', 'xrgty37_new_menu_link', 40 );
function xrgty37_new_menu_link( $menu_links ){
 
	$menu_links = array_slice( $menu_links, 0, 2, true ) 
	+ array( 'new-menu' => 'New Menu' )
	+ array_slice( $menu_links, 2, NULL, true );
 
	return $menu_links;
 
}
/*
 * Step 2. Register Permalink Endpoint
 */
add_action( 'init', 'xrgty37_add_endpoint' );
function xrgty37_add_endpoint() {
 
	// Check WP_Rewrite
	add_rewrite_endpoint( 'new-menu', EP_PAGES );
 
}
/*
 * Step 3. Content for the new page in My Account, woocommerce_account_{ENDPOINT NAME}_endpoint
 */
add_action( 'woocommerce_account_new-menu_endpoint', 'xrgty37_my_account_endpoint_content' );
function xrgty37_my_account_endpoint_content() {
 
	// Content for new page
	echo 'This is content for newly created menu item.';
 
}

Looking for a skilled WordPress/WooCommerce developer? I'm currently available for freelance, contract, or full-time remote opportunities! Let's create something amazing together. Send me a quick message, and I'll respond within 24 hours!

Recent Posts

  1. Automating Code Linting with GitHub Actions for WordPress Plugins
  2. Comprehensive Guide to Linting PHP, JavaScript, and CSS in WordPress Plugins Using Composer
  3. The Ultimate Guide to Indexing in Database Design
  4. Understanding 'update_meta_cache' in WordPress - When to Use It, When Not to, and Its Impact on Database Queries
  5. A Guide to Configuring JavaScript and SCSS Paths in WordPress Plugins with @wordpress/scripts

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.