Customising account page tabs
This guide covers how to manipulate the tabbed "My Account" page endpoints found within WooCommerce 2.6.
First you need to register a new endpoint in WordPress using the add_rewrite_endpoint()
function and also register it via the query_vars
filter.
/**
* Register new endpoint to use inside My Account page.
*
* @see https://developer.wordpress.org/reference/functions/add_rewrite_endpoint/
*/
function my_custom_endpoints() {
add_rewrite_endpoint( 'my-custom-endpoint', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'my_custom_endpoints' );
/**
* Add new query var.
*
* @param array $vars
* @return array
*/
function my_custom_query_vars( $vars ) {
$vars[] = 'my-custom-endpoint';
return $vars;
}
add_filter( 'query_vars', 'my_custom_query_vars', 0 );
It will also be necessary to flush rewrite rules using flush_rewrite_rules()
on plugin or theme activation, or to re-save permalinks via Settings > Permalinks so the new endpoint is active. If you are using flush_rewrite_rules()
on plugin/theme activation, make sure the endpoint is added beforehand.
Example for plugins:
/**
* Flush rewrite rules on plugin activation.
*/
function my_custom_flush_rewrite_rules() {
add_rewrite_endpoint( 'my-custom-endpoint', EP_ROOT | EP_PAGES );
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'my_custom_flush_rewrite_rules' );
register_deactivation_hook( __FILE__, 'my_custom_flush_rewrite_rules' );
Example for themes:
/**
* Flush rewrite rules on theme activation.
*/
function my_custom_flush_rewrite_rules() {
add_rewrite_endpoint( 'my-custom-endpoint', EP_ROOT | EP_PAGES );
flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'my_custom_flush_rewrite_rules' );
It is possible to manipulate the menu items on the account page using the woocommerce_account_menu_items
filter.
The following example shows how to add a new item at the end of the menu before the logout
item:
/**
* Insert the new endpoint into the My Account menu.
*
* @param array $items
* @return array
*/
function my_custom_my_account_menu_items( $items ) {
// Remove the logout menu item.
$logout = $items['customer-logout'];
unset( $items['customer-logout'] );
// Insert your custom endpoint.
$items['my-custom-endpoint'] = __( 'My Custom Endpoint', 'woocommerce' );
// Insert back the logout item.
$items['customer-logout'] = $logout;
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'my_custom_my_account_menu_items' );
It's also possible add items in any position, simply working with the array of items:
/**
* Custom help to add new items into an array after a selected item.
*
* @param array $items
* @param array $new_items
* @param string $after
* @return array
*/
function my_custom_insert_after_helper( $items, $new_items, $after ) {
// Search for the item position and +1 since is after the selected item key.
$position = array_search( $after, array_keys( $items ) ) + 1;
// Insert the new item.
$array = array_slice( $items, 0, $position, true );
$array += $new_items;
$array += array_slice( $items, $position, count( $items ) - $position, true );
return $array;
}
/**
* Insert the new endpoint into the My Account menu.
*
* @param array $items
* @return array
*/
function my_custom_my_account_menu_items( $items ) {
$new_items = array();
$new_items['my-custom-endpoint'] = __( 'My Custom Endpoint', 'woocommerce' );
// Add the new item after `orders`.
return my_custom_insert_after_helper( $items, $new_items, 'orders' );
}
add_filter( 'woocommerce_account_menu_items', 'my_custom_my_account_menu_items' );
WooCommerce creates hooks dynamically for each endpoint, so to manipulate the display of endpoints it is possible to use the woocommerce_account_{$endpoint}_endpoint
hook.
By default WooCommerce has the following hooks for endpoints:
woocommerce_account_orders_endpoint
woocommerce_account_view-order_endpoint
woocommerce_account_downloads_endpoint
woocommerce_account_edit-address_endpoint
woocommerce_account_payment-methods_endpoint
woocommerce_account_add-payment-method_endpoint
woocommerce_account_edit-account_endpoint
/**
* Endpoint HTML content.
*/
function my_custom_endpoint_content() {
echo '<p>Hello World!</p>';
}
add_action( 'woocommerce_account_my-custom-endpoint_endpoint', 'my_custom_endpoint_content' );
To change the page title for the endpoint:
/*
* Change endpoint title.
*
* @param string $title
* @return string
*/
function my_custom_endpoint_title( $title ) {
global $wp_query;
$is_endpoint = isset( $wp_query->query_vars['my-custom-endpoint'] );
if ( $is_endpoint && ! is_admin() && is_main_query() && in_the_loop() && is_account_page() ) {
// New page title.
$title = __( 'My Custom Endpoint', 'woocommerce' );
remove_filter( 'the_title', 'my_custom_endpoint_title' );
}
return $title;
}
add_filter( 'the_title', 'my_custom_endpoint_title' );
WooCommerce is an open source commerce platform built for WordPress and lovingly crafted by Automattic and the WooCommerce community 💜. Come and work with us!
Contribution
- Set up development environment
- Our Git Flow
- SCSS and JS minification
- Naming conventions
- CSS SASS coding guidelines and naming conventions
- Critical Flows
- API Critical Flows
- String localisation guidelines
- Translating WooCommerce
- Deprecation in core
- Adding Actions and Filters
- Common Issues
- Writing high-quality testing instructions
Release Notes
- Release Testing Instructions
- 3.6.x notes/FAQ
- 2.6.x to 3.0.0 Developer Migration Notes
- Select2 fields not working in 3.0.x
- Thumbnail Image Regeneration in 3.3+
- Customizing image sizes in 3.3+
REST API and CLI
CRUD & Data Descriptions
- Database Description
- CRUD Objects in 3.0
- Order and Order Line Item Data
- Coupon Data
- Customer Data
- Product Data
- Data Stores
Internal APIs
Theming
- Enabling product gallery features (zoom, swipe, lightbox)
- Template File Guidelines for Devs and Theme Authors
Examples / Guides