Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sandeshjangam/45c46654cd45c32f7bf02f342a45e37e to your computer and use it in GitHub Desktop.
Save sandeshjangam/45c46654cd45c32f7bf02f342a45e37e to your computer and use it in GitHub Desktop.
WooCommerce - Add New “Tab” at My Account Page
<?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' );
@lacikawiz
Copy link

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:

add_filter('woocommerce_account_menu_item_classes',function($classes,$endpoint){
  if($endpoint=="premium-support") {
    $urlStub="/premium-support/";  //the url should end with this
    if(substr_compare($_SERVER['REQUEST_URI'],$urlStub,-strlen($urlStub))===0) $classes[] = 'is-active';
  }
  return $classes;
},10,2);

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment