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.



