{"id":306,"date":"2020-08-27T17:07:06","date_gmt":"2020-08-27T17:07:06","guid":{"rendered":"https:\/\/demo.wpartisan.net\/developmentsite\/how-can-i-add-a-custom-endpoint-as-a-my-account-page-menu-item\/"},"modified":"2024-11-04T03:45:44","modified_gmt":"2024-11-04T03:45:44","slug":"how-can-i-add-a-custom-endpoint-as-a-my-account-page-menu-item","status":"publish","type":"quick-tips","link":"https:\/\/demo.wpartisan.net\/developmentsite\/how-can-i-add-a-custom-endpoint-as-a-my-account-page-menu-item\/","title":{"rendered":"How can i add a custom endpoint as a my account page menu item?"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">To do this, first, we need to add an endpoint using the WordPress function called add_rewrite_endpoint() using the \\&#8217;init\\&#8217; action hook like:<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">function wpartisan_custom_endpoint() {\n\tadd_rewrite_endpoint( 'custom-menu', EP_PAGES  );\n\tif( !get_option('rw_permalinks_flushed') ) {\n        flush_rewrite_rules(false);\n        update_option('rw_permalinks_flushed', 1);\n \n    }\n}\n\nadd_action( 'init', 'wpartisan_custom_endpoint' );<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the function add_rewrite_endpoint(), the first parameter is the endpoint&#8217;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 &#8220;custom-menu(\/(.*))?\/?$&#8221; for every page. So when you will visit your page URL like https:\/\/{your domain name}\/my-account\/custom-menu\/foo\/, then you will see your global $wp_query object like the below image:<\/p>\n\n\n\n<img decoding=\"async\" src=\"https:\/\/wpartisan.net\/wp-content\/uploads\/2023\/02\/add-query-vars-wp.png\" alt=\"My account custom menu\">\n\n\n\n<p class=\"wp-block-paragraph\">I use the flush_rewrite_rules() function inside the custom endpoint creation function because if you do not flush, then when you try to access this custom menu, it will go to a 404 page. You can do this manually also from Settings-&gt; Permalinks in the admin area.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For more understanding, visit https:\/\/codex.wordpress.org\/Rewrite_API\/add_rewrite_endpoint.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Add a Menu Item<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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 &#8216;woocommerce_account_menu_items&#8217; filter like the below:<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">add_filter( 'woocommerce_account_menu_items', 'wpartisan_new_menu_items' );\nfunction wpartisan_new_menu_items( $items ) {\n\t$items[ 'custom-menu' ] = __( 'Custom Menu', 'wpartisan' );\n\treturn $items;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Add Content for the Menu Item<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now the final step is to show content for this custom endpoint, which means when you visit that URL, which content will be shown for that? For that, WooCommerce provides a hook, and we can use this like below:<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">$endpoint = 'custom-menu';\n\nadd_action( 'woocommerce_account_' . $endpoint .  '_endpoint', 'wpartisan_endpoint_content' );\n\nfunction wpartisan_endpoint_content() {\n\t\/\/echo $args_val = get_query_var( 'custom-menu', 1 );\n    \/\/Custom menu content goes here\n    echo 'Custom menu content goes here';    \n}<\/code><\/pre>\n\n\n\n<img decoding=\"async\" src=\"https:\/\/wpartisan.net\/wp-content\/uploads\/2023\/02\/myaccount-custom-menu-item.png\" alt=\"My account custom menu\">\n\n\n\n<h2 class=\"wp-block-heading\">Menu item order change<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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:<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">add_filter( 'woocommerce_account_menu_items', 'wpartisan_new_menu_items' );\nfunction wpartisan_new_menu_items( $items ) {\n\t\n\t$items = array_slice( $items, 0, 3, true )\n\t+ array( 'custom-menu' => __( 'Custom Menu', 'wpartisan' ) )\n\t+ array_slice( $items, 3, count( $items ), true );\n\n\treturn $items;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then the final output will be like this:<\/p>\n\n\n\n<img decoding=\"async\" src=\"https:\/\/wpartisan.net\/wp-content\/uploads\/2023\/02\/menu-item-order-change.png\" alt=\"My account custom menu item order change\">\n","protected":false},"excerpt":{"rendered":"<p>Here I can show you how we can add a custom endpoint to WooCommerce My Account page, like Orders, Download, Addresses etc., and show the content for that endpoint.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}}},"tags":[],"topics":[43,45],"class_list":["post-306","quick-tips","type-quick-tips","status-publish","hentry","topics-woocommerce","topics-wordpress"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"WPArtisan","author_link":"https:\/\/demo.wpartisan.net\/developmentsite\/author\/developmentsiteauth\/"},"uagb_comment_info":0,"uagb_excerpt":"Here I can show you how we can add a custom endpoint to WooCommerce My Account page, like Orders, Download, Addresses etc., and show the content for that endpoint.","_links":{"self":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/quick-tips\/306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/quick-tips"}],"about":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/types\/quick-tips"}],"author":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/comments?post=306"}],"wp:attachment":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/media?parent=306"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/tags?post=306"},{"taxonomy":"topics","embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/topics?post=306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}