Remove Dashboard from WooCommerce My Account

Remove Dashboard Endpoint Tutorial

In this short tutorial, we’ll remove the Dashboard tab from the WooCommerce My Account area. We’ll also configure the site to use a different default My Account tab, like the “Orders” tab, so even if someone tries to directly access /my-account/, they’ll end up on /my-account/orders/.

Key tasks:

  1. Remove Dashboard from the list of My Account menu items.
  2. Detect when a customer lands on /my-account/ and redirect them to something like /my-account/orders/ or /my-account/downloads/

importantMake sure you’re using a custom child theme so you can edit functions.php.

Remove My Account from WC Dashboard
The WooCommerce Dashboard

Let’s Write Some Code

In your custom child theme’s folder, create a new file called functions-myaccount.php and paste the following into it:

<?php

/**
 * WP Tutorials Nav Menu Icons
 *
 * https://wp-tutorials.tech/refine-wordpress/remove-dashboard-from-woocommerce-my-account/
 *
 */

// Block direct access.
defined('WPINC') || die();

/*
 * Set this to the endpoint you want to use, e.g.:
 * 'orders', 'downloads', 'edit-account'
 */
const WPT_DEFAULT_MYACCOUNT_ENDPOINT = 'orders';

/**
 * Adjust the My Account menu items.
 */
function custom_remove_dashboard_from_myaccount($menu_items, $endpoints) {
	// Remove "Dashboard" from the list of available menu items.
	if (array_key_exists('dashboard', $menu_items)) {
		unset($menu_items['dashboard']);
	}

	return $menu_items;
}
add_filter('woocommerce_account_menu_items', 'custom_remove_dashboard_from_myaccount', 90, 2);

/**
 * Redirect /my-account/ to /my-account/WPT_DEFAULT_MYACCOUNT_ENDPOINT/
 */
function custom_redirect_from_dashboard() {
	$dashboard_query_path = '';
	$current_query_path = '';

	if (is_user_logged_in() && function_exists('wc_get_account_endpoint_url')) {
		// Get the Dashboard (my-account) query path.
		// This is usually "/my-account/"
		$dashboard_query_path = trailingslashit(parse_url(wc_get_account_endpoint_url('dashboard'), PHP_URL_PATH));

		// Get this query's path, which could be anything:
		// /my-account/
		// /my-account/orders/
		// /some-other-content/
		global $wp;
		$current_url = home_url($wp->request);
		$current_query_path = trailingslashit(parse_url($current_url, PHP_URL_PATH));
	}

	if (empty($dashboard_query_path) || empty($current_query_path)) {
		// If we don't know what the dashboard's query path is, don't do
		// anything. Maybe WooCommerce isn't installed.
	} elseif ($dashboard_query_path !== $current_query_path) {
		// If the current query path is NOT the dashboard query path,
		// don't do anything special here.
	} elseif (empty($redirect_to_url = wc_get_account_endpoint_url(WPT_DEFAULT_MYACCOUNT_ENDPOINT))) {
		// If we can't get the URL of redirect-to endpoint we want, don't
		// do anything.
	} else {
		// Redirect the browser: $redirect_to_url
		wp_safe_redirect($redirect_to_url);
		exit();
	}
}
add_action('template_redirect', 'custom_redirect_from_dashboard');

Now open your child theme’s functions.php and add the following couple of lines:

// Adjust WooCommerce My Account
require_once dirname(__FILE__) . '/functions-myaccount.php';

Save everything and check that your site still works.

How it Works

Breaking down the logic is easy in this one.

We use the woocommerce_account_menu_items filter to modify the array of available My Account menu items. This is also what we do in our tutorial on how to hide My Account endpoints by user role.

The “template_redirect” action fires after WooCommerce has finished initialising. In here, we run a few tests to find out if the user is logged-in and trying to view the dashboard… so they’re trying to view an account page, but not an account page that’s on an endpoint.

Menu ItemSlugAccount EndpointSite Path
Dashboarddashboard/my-account/
Ordersordersorders/my-account/orders/
Addressedit-accountedit-account/my-account/edit-account/
Some WooCommerce My Account endpoints

When we detect that the user is logged-in and trying to view the dashboard, we grab the URL of the endpoint we actually want and call wp_safe_redirect(). This sends the user to the correct place, without them realising they’ve even been redirected.

That’s all there is to it. Happy dashboard-ing! 😎 👍

Like This Tutorial?

Let us know

WordPress plugins for developers

4 thoughts on “Remove Dashboard from WooCommerce My Account”

  1. hi, when i activate this snippet, endpoint ‘points’ of Points and Rewards not functionin as it should be. When I click the Points it divert to orders again. Why is that?

    Reply

Leave a comment