Last active
April 14, 2022 08:17
-
-
Save sandeshjangam/45c46654cd45c32f7bf02f342a45e37e to your computer and use it in GitHub Desktop.
WooCommerce - Add New “Tab” at My Account Page
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
<?php | |
/** | |
* @snippet WooCommerce How To Add New Tab @ My Account | |
* @how-to Watch tutorial @ https://techiesandesh.com | |
* @author Sandesh Jangam | |
* @compatible WooCommerce 3.6.2 | |
* @donate $7 https://www.paypal.me/SandeshJangam/7 | |
*/ | |
/** | |
* 1. Register new endpoint slug to use for My Account page | |
*/ | |
/** | |
* @important-note Resave Permalinks or it will give 404 error | |
*/ | |
function ts_custom_add_premium_support_endpoint() { | |
add_rewrite_endpoint( 'premium-support', EP_ROOT | EP_PAGES ); | |
} | |
add_action( 'init', 'ts_custom_add_premium_support_endpoint' ); | |
/** | |
* 2. Add new query var | |
*/ | |
function ts_custom_premium_support_query_vars( $vars ) { | |
$vars[] = 'premium-support'; | |
return $vars; | |
} | |
add_filter( 'woocommerce_get_query_vars', 'ts_custom_premium_support_query_vars', 0 ); | |
/** | |
* 3. Insert the new endpoint into the My Account menu | |
*/ | |
function ts_custom_add_premium_support_link_my_account( $items ) { | |
$items['premium-support'] = 'Premium Support'; | |
return $items; | |
} | |
add_filter( 'woocommerce_account_menu_items', 'ts_custom_add_premium_support_link_my_account' ); | |
/** | |
* 4. Add content to the new endpoint | |
*/ | |
function ts_custom_premium_support_content() { | |
echo '<h3>Premium WooCommerce Support</h3><p>Welcome to the WooCommerce support area. As a premium customer, you can submit a ticket should you have any WooCommerce issues with your website, snippets or customization. <i>Please contact your theme/plugin developer for theme/plugin-related support.</i></p>'; | |
echo do_shortcode( ' /* your shortcode here */ ' ); | |
} | |
/** | |
* @important-note "add_action" must follow 'woocommerce_account_{your-endpoint-slug}_endpoint' format | |
*/ | |
add_action( 'woocommerce_account_premium-support_endpoint', 'ts_custom_premium_support_content' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this gist! The note about having to re-save the Permalink settings got me unstuck.
The only thing that I found missing is that the newly created menu item will not receive the
is-active
class when it's active. This requires one more filter:PS: I prefer the anonymous function for one-time-use functions. But the above can be re-written easily to use the more conventional format.