Adding a New Tab Under the WooCommerce My Account Page with a Shortcode: A Step-by-Step Guide

WooCommerce is a popular e-commerce platform built on top of WordPress. It is widely used for creating online stores and managing sales, orders, and customers. One of the key features of WooCommerce is its My Account page, which provides customers with a personalized area where they can manage their orders, view their account details, and access other useful features.

In this article, we will learn how to add a new tab under the WooCommerce My Account page and display a shortcode in the new tab. We will use only code to accomplish this, so make sure you are comfortable working with WordPress code before proceeding.

Step 1: Create a New WordPress Page

The first step is to create a new WordPress page that will contain the content we want to display in the new tab. Go to Pages -> Add New and give your new page a title and some content. You can use any content you like for this tutorial, but for demonstration purposes, we will use a simple shortcode that displays the current date.

				
					<?php
/**
 * Plugin Name: Add New Tab to WooCommerce My Account
 * Plugin URI: https://yourwebsite.com
 * Description: Adds a new tab to the WooCommerce My Account page and displays a shortcode.
 * Version: 1.0.0
 * Author: Your Name
 * Author URI: https://yourwebsite.com
 */
 
// Add new endpoint to the My Account page
function my_account_add_new_endpoint() {
    add_rewrite_endpoint( 'new-tab', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'my_account_add_new_endpoint' );
 
// Add new tab to the My Account page
function my_account_new_tab( $items ) {
    // Add new menu item
    $items['new-tab'] = __( 'New Tab', 'woocommerce' );
    return $items;
}
add_filter( 'woocommerce_account_menu_items', 'my_account_new_tab' );
 
// Add content to the new tab
function my_account_new_tab_content() {
    // Output shortcode
    echo do_shortcode( '[current_date]' );
}
add_action( 'woocommerce_account_new-tab_endpoint', 'my_account_new_tab_content' );
 
// Register the shortcode
function current_date_shortcode() {
    return date( 'F j, Y' );
}
add_shortcode( 'current_date', 'current_date_shortcode' );

				
			

Let’s take a closer look at each of these functions.

Step 2: Add a New Endpoint to the My Account Page

In this step, we will add a new endpoint to the WooCommerce My Account page. An endpoint is a special URL that can be used to access a specific page or function on your website. We will use this endpoint to create a new tab on the My Account page.

To add a new endpoint, we will use the add_rewrite_endpoint function. This function takes two arguments: the name of the endpoint and the URL structure. In our case, we will use the name new-tab and the EP_ROOT | EP_PAGES constants to define the URL structure.

				
					// Add new endpoint to the My Account page
function my_account_add_new_endpoint() {
    add_rewrite_endpoint( 'new-tab', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'my_account_add_new_endpoint' );

				
			

Step 3: Add a New Tab to the My Account Page

Now that we have created a new endpoint, we can use it to add a new tab to the WooCommerce My Account page. To do this, we will use the woocommerce_account_menu_items filter. This filter allows us to modify the list of menu items on the My Account page.

We will create a new function called my_account_new_tab that takes the current menu items as an argument and adds a new item to the list. We will use the __() function to provide a translatable string for the menu item.

				
					// Add new tab to the My Account page
function my_account_new_tab( $items ) {
    // Add new menu item
    $items['new-tab'] = __( 'New Tab', 'woocommerce' );
    return $items;
}
add_filter( 'woocommerce_account_menu_items', 'my_account_new_tab' );

				
			

Step 4: Add Content to the New Tab

Now that we have added a new tab to the My Account page, we can add content to it. To do this, we will create a new function called my_account_new_tab_content that outputs the content we want to display.

In our case, we will use the do_shortcode() function to execute a shortcode that displays the current date. We will register this shortcode in a later step.

				
					// Add content to the new tab
function my_account_new_tab_content() {
    // Output shortcode
    echo do_shortcode( '[current_date]' );
}
add_action( 'woocommerce_account_new-tab_endpoint', 'my_account_new_tab_content' );

				
			

Step 5: Register the Shortcode

In order to use the [current_date] shortcode in our new tab, we need to register it. We will create a new function called current_date_shortcode that returns the current date using the date() function.

				
					// Register the shortcode
function current_date_shortcode() {
    return date( 'F j, Y' );
}
add_shortcode( 'current_date', 'current_date_shortcode' );

				
			

Step 6: Test the New Tab

Now that we have added a new tab to the My Account page and registered a shortcode to display the content, we can test our code.

First, make sure you have saved the code in a PHP file and uploaded it to your WordPress plugin directory. Then, go to the WooCommerce My Account page and you should see a new tab called “New Tab” with the current date displayed.

Conclusion

In this article, we have learned how to add a new tab under the WooCommerce My Account page and display a shortcode in the new tab using only code. By following the steps outlined in this tutorial, you should now have a better understanding of how to modify the WooCommerce My Account page and create custom functionality for your online store.

Keep in mind that this code can be modified and extended to add additional tabs or functionality to your WooCommerce My Account page. You can add custom content or functionality to the new tab by modifying the my_account_new_tab_content() function.

Additionally, you can modify the endpoint name and URL structure by changing the arguments passed to the add_rewrite_endpoint() function. This will change the URL of the new tab and allow you to create additional endpoints for other custom functionality.

It is also important to note that modifying the core functionality of WooCommerce can be risky, as it can cause conflicts with other plugins or future updates. It is recommended to use a child theme or create a separate plugin to house your custom code, so that your changes will not be overwritten during updates.

In conclusion, adding a new tab under the WooCommerce My Account page and displaying a shortcode in the new tab can be accomplished with just a few lines of code. With the steps outlined in this article, you can easily create custom functionality for your online store and enhance the user experience for your customers.

Keep in mind that we can combine this blog post with our last blog post on WooCommerce Tips and Tricks: Shortcode for Displaying All Products Purchased by User to have a stellar user feature.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Still Have Questions?

If you didn’t find the answer to your questions, reach out to us – we’re happy to help! 

Software Development

From mobile apps to website development, let us help you build your next solution

Fully Managed Hosting

We manage all aspects of hosting so you can focus on your business

Weekly Billing

No surprise bills. Each week you get a detailed list of the work done down to the minute

AI Experience

Experience in using AI (Artifical Inteligance) to improve business profits