Add new tab to woocommerce my account page

/**
WooCommerce Add New Tab @ My Account
*/
  
// 1. Register new endpoint (URL) for My Account page
  
function my_files_add_files_endpoint() {
    add_rewrite_endpoint( 'my-files', EP_ROOT | EP_PAGES );
}
  
add_action( 'init', 'my_files_add_files_endpoint' );
  
// ------------------
// 2. Add new query var
  
function my_files_files_query_vars( $vars ) {
    $vars[] = 'my-files';
    return $vars;
}
  
add_filter( 'query_vars', 'my_files_files_query_vars', 0 );
  
// ------------------
// 3. Insert the new endpoint into the My Account menu
  
function my_files_add_files_link_my_account( $items ) {
    $items['my-files'] = 'My files';
    return $items;
}
  
add_filter( 'woocommerce_account_menu_items', 'my_files_add_files_link_my_account' );
  
// ------------------
// 4. Add content to the new tab
  
function my_files_files_content() {
   echo '<h4>Tab title</h4>';
   echo do_shortcode( ' [custom-shortcode] ' );
}
  
add_action( 'woocommerce_account_my-files_endpoint', 'my_files_files_content' );
// Note: add_action must follow 'woocommerce_account_{your-endpoint-slug}_endpoint' format

// ------------------
// 5. Reorder new tab to a new position 
add_filter( 'woocommerce_account_menu_items', 'bbloomer_add_link_my_account' );
 
function bbloomer_add_link_my_account( $items ) {
   $save_for_later = array( 'my-files' => __( 'My files', 'woocommerce' ) ); // SAVE TAB
   unset( $items['my-files'] ); // REMOVE TAB
   $items = array_merge( array_slice( $items, 0, 5 ), $save_for_later, array_slice( $items, 2 ) ); // PLACE TAB AFTER POSITION 5
   return $items;
}

Leave a Reply