How to Add Custom Endpoints to Woocommerce Account Pages?

If you’re looking to add custom endpoints to your WooCommerce site without a hitch, you might feel a bit overwhelmed by where to put your code. In this post, I’ll show you two easy ways to add the custom code snippet below to your site.

Video: How to Add Custom Endpoints to Woocommerce Account Pages?

Why Custom Endpoints Are Useful?

Custom endpoints let you add unique features to your WooCommerce account pages, making it easier to provide extra value for your customers. Here are a few practical reasons and use cases:

Display Contact Information:

Create a dedicated endpoint for support or contact details. Customers can quickly access help or FAQs directly from their account page.

Track Orders and Shipments:

Use custom endpoints to show detailed order tracking information. This can include shipping updates or delivery status, giving your customers real-time insights into their orders.

Personalized Content:

Tailor specific content for logged-in users. For instance, add a section with custom promotions, loyalty points, or personalized recommendations.

Collect Customer Feedback:

Set up a form on a custom endpoint that lets users provide feedback. This direct line to customer opinions can help you improve your service.

Monitor User Activities:

Integrate tracking or analytics on an endpoint. This can be useful for understanding customer behavior, helping you make informed decisions about future improvements.

By using custom endpoints, you can enhance the user experience without cluttering your main site. They allow you to keep your account page organized while providing extra, useful functionality that benefits both you and your customers.

Method 1: Using Your Theme’s functions.php File

1. Backup Your Site:

Before you make any changes, always back up your site. This way, you can restore your site if something goes wrong.

2. Access Your Theme Editor:

Log into your WordPress admin dashboard. Go to Appearance > Theme File Editor. Locate your theme’s functions.php file.

3. Paste the Code (see below):

Scroll to the bottom of the functions.php file and paste the code snippet. Save the file once you’re done.

Tip: Editing functions.php directly can be risky. If you update or change your theme, you might lose these custom changes.

Method 2: Using the WPCode Lite Plugin

1. Install WPCode Lite:

From your WordPress dashboard, go to Plugins > Add New. Search for “WPCode Lite,” install it, and then activate the plugin.

2. Add a New Snippet:

In your dashboard, look for the WPCode Lite menu (usually called “Code Snippets” or similar). Click to add a new snippet.

3. Paste and Save Your Code:

Create a new snippet, give it a name like “Custom Endpoints for WooCommerce,” and paste your code into the provided box. Set the snippet to run everywhere (or choose where you’d like it to run), then save and activate it.

Why Use WPCode Lite?

Safer Management: Plugins like WPCode Lite let you add custom code without altering theme files. This minimizes the risk of breaking your site.

Easy Updates: When you switch themes or update them, your custom code remains intact since it’s stored in the plugin.

Organized Snippets: WPCode Lite helps you keep track of all your custom code snippets in one place. It’s easier to manage, update, or deactivate specific codes as needed.

Quick Troubleshooting: If something goes wrong, you can disable the snippet directly from the plugin without digging into files.

The Code You Need to Add Custom Endpoints to Woocommerce Account Pages

Here’s the code snippet that registers a “Custom Endpoints” custom post type with Gutenberg support for WooCommerce. It creates new endpoints on the My Account page, handles query variables, and flushes rewrite rules automatically on activation.

// Add Custom Endpoints to Woocommerce Account Pages
// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Register 'Custom Endpoints' Custom Post Type with Gutenberg Support
 */
function register_custom_endpoints_cpt() {
    $args = array(
        'labels' => array(
            'name'               => __('Custom Endpoints', 'your-textdomain'),
            'singular_name'      => __('Custom Endpoint', 'your-textdomain'),
            'menu_name'          => __('Custom Endpoints', 'your-textdomain'),
            'add_new'            => __('Add New', 'your-textdomain'),
            'add_new_item'       => __('Add New Custom Endpoint', 'your-textdomain'),
            'edit_item'          => __('Edit Custom Endpoint', 'your-textdomain'),
            'new_item'           => __('New Custom Endpoint', 'your-textdomain'),
        ),
        'public'        => false,
        'show_ui'       => true,
        'show_in_menu'  => 'woocommerce',
        'capability_type' => 'post',
        'hierarchical'  => false,
        'supports'      => array('title', 'editor'),
        'menu_icon'     => 'dashicons-admin-links',
        'show_in_rest'  => true, // Enables Gutenberg support
    );

    register_post_type('custom_wc_endpoint', $args);
}
add_action('init', 'register_custom_endpoints_cpt');

/**
 * Register Endpoint Query Variable
 */
function add_custom_wc_query_vars($vars) {
    $vars[] = 'custom_wc_endpoint';
    return $vars;
}
add_filter('query_vars', 'add_custom_wc_query_vars');

/**
 * Add Custom Endpoints to WooCommerce My Account Page
 */
function add_custom_wc_endpoints() {
    $args = array(
        'post_type'      => 'custom_wc_endpoint',
        'posts_per_page' => -1,
        'post_status'    => 'publish',
    );
    
    $endpoints = get_posts($args);
    
    foreach ($endpoints as $endpoint) {
        $slug = sanitize_title($endpoint->post_title);
        add_rewrite_endpoint($slug, EP_ROOT | EP_PAGES);
    }
    
    flush_rewrite_rules(); // Auto flush permalinks
}
add_action('init', 'add_custom_wc_endpoints', 10);

/**
 * Register Custom Endpoints with WooCommerce My Account Menu (Before Log Out)
 */
function register_custom_wc_account_menu_items($items) {
    $logout = $items['customer-logout'];
    unset($items['customer-logout']);
    
    $args = array(
        'post_type'      => 'custom_wc_endpoint',
        'posts_per_page' => -1,
        'post_status'    => 'publish',
    );
    
    $endpoints = get_posts($args);
    
    foreach ($endpoints as $endpoint) {
        $slug = sanitize_title($endpoint->post_title);
        if (!empty($slug)) {
            $items[$slug] = esc_html($endpoint->post_title);
        }
    }
    
    $items['customer-logout'] = $logout; // Move Log Out to the bottom
    
    return $items;
}
add_filter('woocommerce_account_menu_items', 'register_custom_wc_account_menu_items', 20);

/**
 * Display Custom Endpoint Content on My Account Page
 */
function display_custom_wc_endpoint_content() {
    global $wp;

    // Get all custom endpoint slugs
    $args = array(
        'post_type'      => 'custom_wc_endpoint',
        'posts_per_page' => -1,
        'post_status'    => 'publish',
        'fields'         => 'ids', // Optimize query by only fetching IDs
    );

    $endpoints = get_posts($args);
    $endpoint_slugs = array();

    foreach ($endpoints as $endpoint_id) {
        $endpoint_slugs[] = sanitize_title(get_the_title($endpoint_id));
    }

    // Find the custom endpoint slug in the query vars
    $slug = '';
    foreach ($wp->query_vars as $key => $value) {
        if (in_array($key, $endpoint_slugs)) {
            $slug = $key;
            break;
        }
    }

    if (empty($slug)) {
        echo '<h2>Oops! Content not found</h2>';
        return;
    }

    // Get the custom endpoint post by slug
    $args = array(
        'name'           => $slug,
        'post_type'      => 'custom_wc_endpoint',
        'post_status'    => 'publish',
        'posts_per_page' => 1,
    );

    $custom_endpoint = get_posts($args);

    if (!empty($custom_endpoint)) {
        echo '<div class="woocommerce-custom-endpoint-content">';
        echo apply_filters('the_content', $custom_endpoint[0]->post_content);
        echo '</div>';
    } else {
        echo '<h2>Oops! Content not found</h2>';
    }
}

/**
 * Hook Endpoint Content Display to WooCommerce My Account Page
 */
function hook_custom_wc_endpoints() {
    $args = array(
        'post_type'      => 'custom_wc_endpoint',
        'posts_per_page' => -1,
        'post_status'    => 'publish',
    );

    $endpoints = get_posts($args);

    foreach ($endpoints as $endpoint) {
        $slug = sanitize_title($endpoint->post_title);
        add_action('woocommerce_account_' . $slug . '_endpoint', 'display_custom_wc_endpoint_content');
    }
}
add_action('init', 'hook_custom_wc_endpoints');

/**
 * Flush Rewrite Rules on Activation
 */
function force_flush_permalinks_on_activation() {
    add_custom_wc_endpoints();
    flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'force_flush_permalinks_on_activation');

Give it a try, and you’ll have those custom WooCommerce endpoints running smoothly in no time!

Final Thoughts

Both methods will get the job done, but for beginners and even intermediate users, WPCode Lite is often the safer and more organized option. It keeps your custom code separate from your theme files, so you don’t risk losing your work during updates or theme changes.

Do you want to thank me and buy me a beer?

Every donation is entirely welcome but NEVER required. Enjoy my work for free but if you would like to thank me and buy me a beer or two then you can use this form here below.

Donation Form (#2)

Here are some of my favorite WordPress tools

Thanks for reading this article! I hope it's been useful as you work on your own websites and e-commerce sites. I wanted to share some tools I use as a WordPress developer, and I think you'll find them helpful too.

Just so you know, these are affiliate links. If you decide to use any of them, I'll earn a commission. This helps me create tutorials and YouTube videos. But honestly, I genuinely use and recommend these tools to my friends and family as well. Your support keeps me creating content that benefits everyone.

Themes: Over the past few years, I've consistently relied on two primary themes for all sorts of projects: the Blocksy theme and the Kadence Theme. If you explore this website and my YouTube channel, you'll come across numerous tutorials that delve into these themes. If you're interested in obtaining a 10% discount for both of these themes, then:

Code Snippets Manager: WPCodeBox allows you to add code snippets to your site. Not only that, but it also provides you with the capability to construct and oversee your WordPress Code Snippets library right in the cloud. You can grab it with the 20% discount here (SAVE 20% Coupon: WPSH20).

Contact forms: There are hundreds of contact forms out there but Fluent Forms is the one I like the most. If you need a 20% discount then use this link (save 20% coupon is WPSH20).

Gutenberg add-ons: If I need a good Gutenberg blocks add-on then Kadence Blocks is the one I have used the most. You’ll get a 10% discount with the coupon SIMPLEHACKS here.

Website migration: While building a website you probably need a good plugin that can help you with the migration, backups, restoration, and staging sites. Well, WpVivid is the one I have used for the last couple of years. If you use this link along with the WPSH20 coupon you’ll get a 20% discount.

Woocommerce extensions: There are a bunch of Woocommerce extensions that I like but the one that stands out is Advanced Dynamic Pricing. Once again, you’ll get a 20% discount if you use this link here (save 20% coupon is WPSH20)

Web Hosting: If you would like to have a really fast and easy-to-use managed cloud hosting, then I recommend Verpex Hosting (see my review here). By the way, this site is hosted in Verpex.)

To see all my most up-to-date recommendations, check out this resource that I made for you!

Janek T.
Janek T.

- I have been passionate about Wordpress since 2011, creating websites and sharing valuable tips on using Wordpress and Woocommerce on my site.
- Be the first to receive notifications about new tutorials by subscribing to my Youtube channel .
- Follow me on Twitter here

Articles: 162