Reading list Switch to dark mode

    WooCommerce My Account Custom Endpoint

    Updated 31 May 2023

    WooCommerce Account Page Custom Endpoint – In this post, we will see how we can add custom endpoint to my account page in front means one new our own page like order, download (default one’s).

    To do this, first we’ll add rewrite endpoint using WordPress function ‘add_rewrite_endpoint’ on ‘init’ hook.

    <?php
    
    /**
    *   Plugin Name: WooCommerce Custom Endpoint Webkul
    *   Description: An example plugin to add custom endpoint to My Account Page.
    *   Author: Webkul
    *   Author URI: https://webkul.com
    *   Plugin URI: https://webkul.com
    *   Text Domain: webkul
    */
    
    
    function wk_custom_endpoint() {
      add_rewrite_endpoint( 'custom', EP_ROOT | EP_PAGES );
    }
    
    add_action( 'init', 'wk_custom_endpoint' );
    
    ?>

    In add_rewrite_endpoint function first parameter is name of endpoint and second is places where endpoint will add. We add endpoint to root and pages. The endpoint name is added as query variable. You can explore more places and about this function using below link –

    Reference – https://codex.wordpress.org/Rewrite_API/add_rewrite_endpoint.

    Now, we will add menu item for this custom endpoint on WooCommerce My Account page menu so that we can access easily. For this we’ll use ‘woocommerce_account_menu_items’ filter.

    Searching for an experienced
    WordPress Company ?
    Find out More
    add_filter( 'woocommerce_account_menu_items', 'wk_new_menu_items' );
    
    /**
    * Insert the new endpoint into the My Account menu.
    *
    * @param array $items
    * @return array
    */
    function wk_new_menu_items( $items ) {
    	$items[ 'custom' ] = __( 'Custom', 'webkul' );
    	return $items;
    }
    

    The above code will add one more menu item named Custom at the end of my account page menu list.

    The endpoint has been added, menu item also added, now next thing will be, how can we add content to this new (say) page..? Not to worry, WooCommerce provide the hook using which we can add content –

    $endpoint = 'custom';
    
    add_action( 'woocommerce_account_' . $endpoint .  '_endpoint', 'wk_endpoint_content' );
    
    function wk_endpoint_content() {
        //content goes here
        echo '//content goes here';    
    }
    

    Support

    Still have any issue feel free to add a ticket and let us know your views to make the code better https://webkul.uvdesk.com/en/customer/create-ticket/

    Thanks for Your Time! Have a Good Day!

    . . .

    Leave a Comment

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


    6 comments

  • MrCode
    Not working any more. Not getting any new menu items and no content displayed in WC 4.0
  • Alex Paul
    Thank you for sharing a tutorial to add a custom endpoint to account page. Can you tell me how to add a field in the Registration form? I have been stuck in this code

    function wooc_save_extra_register_fields( $customer_id ) {
    if ( isset( $_POST[‘billing_phone’] ) ) {
    // Phone input filed which is used in WooCommerce
    update_user_meta( $customer_id, ‘billing_phone’, sanitize_text_field( $_POST[‘billing_phone’] ) );
    }
    if ( isset( $_POST[‘billing_first_name’] ) ) {
    //First name field which is by default
    update_user_meta( $customer_id, ‘first_name’, sanitize_text_field( $_POST[‘billing_first_name’] ) );
    // First name field which is used in WooCommerce
    update_user_meta( $customer_id, ‘billing_first_name’, sanitize_text_field( $_POST[‘billing_first_name’] ) );
    }
    if ( isset( $_POST[‘billing_last_name’] ) ) {
    // Last name field which is by default
    update_user_meta( $customer_id, ‘last_name’, sanitize_text_field( $_POST[‘billing_last_name’] ) );
    // Last name field which is used in WooCommerce
    update_user_meta( $customer_id, ‘billing_last_name’, sanitize_text_field( $_POST[‘billing_last_name’] ) );
    }
    }
    add_action( ‘woocommerce_created_customer’, ‘wooc_save_extra_register_fields’ );

    I have used this article as reference https://www.cloudways.com/blog/add-woocommerce-registration-form-fields/

  • dcrecm
    Hello thanks for this tutorial, can you give me another information, how to make it also appear in configuration -> account – this endpoint in woocommerce?

    And about the content, I wanted to make there appear the content of courses bought by the sensei plugin, could it be possible to add a page to show?

    • Webkul Support
      Hello, you’re welcome. For your first point we will write one more post regarding that and second about the content at added endpoint in front-end, the action hook ‘woocommerce_account_’ . $endpoint . ‘_endpoint’ described in last code snippet in this blog will be used.
  • Thakur Amit Chauhan
    Great articles looking forward for some more useful info in near future much applauded
    • Webkul Support
      Thank You
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home

    Table of Content