Step-by-Step: Add a Custom Endpoint to My Account Page
Step 1: Register a Custom Endpoint
To do this, first, we need to add an endpoint using the WordPress function called add_rewrite_endpoint() using the ‘init’ action hook like:
function wpartisan_custom_endpoint() {
add_rewrite_endpoint( 'custom-menu', EP_PAGES );
if( !get_option('rw_permalinks_flushed') ) {
flush_rewrite_rules(false);
update_option('rw_permalinks_flushed', 1);
}
}
add_action( 'init', 'wpartisan_custom_endpoint' );
In the function add_rewrite_endpoint(), the first parameter is the endpoint’s name, and the second parameter means where the endpoint will add as query arguments. For example, EP_PAGES means it will add a new rewrite rule ending with “custom-menu(/(.*))?/?$” for every page. So when you visit a URL like https://{your domain name}/my-account/custom-menu/foo/, you will see the global $wp_query object as shown in the image below.
I use the flush_rewrite_rules() function inside the custom endpoint creation function because without flushing, accessing the custom menu will result in a 404 error. You can do this manually also from Settings-> Permalinks in the admin area.
For more understanding, visit https://codex.wordpress.org/Rewrite_API/add_rewrite_endpoint.
Step 2: Add the Endpoint to My Account Menu
Now, we need to add a menu item for this custom endpoint on WooCommerce My Account page menu. To achieve this feature, we need to use the ‘woocommerce_account_menu_items’ filter like the below:
add_filter( 'woocommerce_account_menu_items', 'wpartisan_new_menu_items' );
function wpartisan_new_menu_items( $items ) {
$items[ 'custom-menu' ] = __( 'Custom Menu', 'wpartisan' );
return $items;
}
Step 3: Display Content for the Custom Endpoint
The final step is to display content for this custom endpoint, meaning that when you visit the URL, the specified content will appear. For that, WooCommerce provides a hook, and we can use this like below:
$endpoint = 'custom-menu';
add_action( 'woocommerce_account_' . $endpoint . '_endpoint', 'wpartisan_endpoint_content' );
function wpartisan_endpoint_content() {
//echo $args_val = get_query_var( 'custom-menu', 1 );
//Custom menu content goes here
echo 'Custom menu content goes here';
}
Menu item order change
In addition, if you want to modify the navigation menu order of the My Account page for the newly created menu item, you need to add the code like the one below:
add_filter( 'woocommerce_account_menu_items', 'wpartisan_new_menu_items' );
function wpartisan_new_menu_items( $items ) {
$items = array_slice( $items, 0, 3, true )
+ array( 'custom-menu' => __( 'Custom Menu', 'wpartisan' ) )
+ array_slice( $items, 3, count( $items ), true );
return $items;
}
Then the final output will be like this:
Plugins to Enhance Your WooCommerce Store
Carefully selected tools to help you customize, manage, and optimize your WooCommerce experience.
Woo Parent Category Selector
- Automatically assign parent categories when child categories are selected
- Prevent accidental removal of required parent categories
- Automatically repairs missing parent category assignments during product save
- Bulk repair existing WooCommerce product category structures
- Keeps product category structures organized and consistent
Product Catalog Mode For WooCommerce
- Turn your WooCommerce store into catalog mode instantly
- Hide Add to Cart buttons and product prices conditionally
- Apply rules by user role, country, or scheduled time periods
- Display custom messages and inquiry options for products
- Control visibility settings globally or per product
Remove Add to Cart Button for WooCommerce
- Remove Add to Cart buttons from shop and product pages
- Hide buttons conditionally for selected products or categories
- Replace Add to Cart with custom text or messages
- Control button visibility by user role and product type
- Lightweight settings interface with easy customization
Remove Product Content for WooCommerce
- Hide product title, image, price, rating, and descriptions
- Remove tabs, meta information, SKU, tags, and related products
- Control visibility separately for shop and single product pages
- Customize WooCommerce layouts without coding
- Enable or disable content elements with simple settings
Really Simple XML and HTML Sitemap
- Generate both XML and HTML sitemaps automatically
- Display sitemaps anywhere using ShortCode support
- Include pages, posts, and custom post types
- Exclude selected pages or posts easily
- Supports WPML and Polylang multilingual plugins
