How to create custom endpoints for WooCommerce in PHP in WordPress

Contents

Introduction

This article explains, in depth, how to create custom endpoints for WooCommerce in PHP. It covers two common cases:

  • Frontend My Account endpoints — add new pages inside the WooCommerce My Account area (URL: /my-account/your-endpoint/), output content and optionally accept parameters.
  • REST API endpoints — add or extend endpoints in the WordPress REST API (namespace your plugin or use the WooCommerce namespace) to serve JSON data for external apps or advanced integrations.

You will find code examples for each step (ready-to-paste PHP code) and explanations for rewrite rules, query vars, menu integration, templates, security, routing with parameters, and debugging tips.

Prerequisites

  • WordPress and WooCommerce installed and active.
  • Ability to add a plugin file or edit your themes functions.php (prefer a plugin for portability).
  • Permalinks enabled (pretty permalinks). You will sometimes need to flush rewrite rules.
  • Basic knowledge of actions/filters and PHP.

High-level overview

  • For My Account endpoints we register a rewrite endpoint using add_rewrite_endpoint, expose the query var, hook into WooCommerces account rendering action (woocommerce_account_{endpoint}_endpoint) and add link in My Account menu via woocommerce_account_menu_items.
  • For parameterized routes we either use add_rewrite_endpoint for a simple slug or add_rewrite_rule to capture components and map to named query vars.
  • For REST endpoints we use register_rest_route inside rest_api_init. Use a proper namespace (for example myplugin/v1), define methods, callbacks and permission callbacks. If integrating with WooCommerce REST namespace use wc/v3 with caution (version compatibility).

Part A — Adding a custom My Account endpoint (step-by-step)

This section shows how to add a basic endpoint called custom-endpoint that appears under My Account, can render a template, and works with WooCommerce-provided navigation and URLs.

1) Register the endpoint (init)

Use add_rewrite_endpoint inside init. Use EP_ROOT EP_PAGES (or EP_ROOTEP_PAGESEP_PERMALINK for safety). Add a query var later.


Note: After registering new rewrite endpoints you must flush rewrite rules. For development you can visit Settings → Permalinks and click Save, or call flush_rewrite_rules() on plugin activation.

2) Expose the query var

WordPress must be told the query var name so you can access it via get_query_var().


3) Add item to My Account menu

Use the filter woocommerce_account_menu_items to add a clickable link in the account navigation. Use wc_get_endpoint_url to build the URL.

 value ) {
        if ( key === customer-logout ) {
            // Insert our endpoint before logout
            new_items[custom-endpoint] = __( Custom Endpoint, myplugin-textdomain )
        }
        new_items[ key ] = value
    }
    // In case logout not found, add to end
    if ( ! isset( new_items[custom-endpoint] ) ) {
        new_items[custom-endpoint] = __( Custom Endpoint, myplugin-textdomain )
    }
    return new_items
}
add_filter( woocommerce_account_menu_items, myplugin_add_endpoint_to_menu )
?>

4) Render endpoint content

WooCommerce fires an action named woocommerce_account_{endpoint}_endpoint when rendering the endpoint page. Use that to output HTML or load a template.


5) Generate the URL programmatically

To link to your endpoint from anywhere, use wc_get_endpoint_url. This respects account base slugs and permalinks.


6) Flushing rewrite rules (activation hook)

Dont call flush_rewrite_rules() on every page load. Run it once when the plugin is activated. Example plugin activation snippet:


7) Using templates and theme overrides

If you prefer a template file rather than echoing HTML in the hook callback, call wc_get_template with the appropriate path and place a template in your theme under woocommerce/myaccount/.


Create the file: your-theme/woocommerce/myaccount/custom-endpoint.php and put markup there. This enables theme-friendly overrides like other WooCommerce templates.

Part B — Adding endpoints that accept parameters (slugs or IDs)

If you need URLs like /my-account/custom-endpoint/123/ (where 123 is an order ID or item ID), use add_rewrite_rule to capture the numeric part and map it to a distinct query var. Then read it with get_query_var().

1) Add rewrite rule and query var




Acepto donaciones de BAT's mediante el navegador Brave :)



Leave a Reply

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