WooCommerce Account Page Custom Endpoint

We can do it using below simple steps. We have to add the below code given in steps in our theme functions.php or in plugin code.

Step 1: 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.

function aft_custom_endpoint() {
  add_rewrite_endpoint( 'custom', EP_ROOT | EP_PAGES );
}
 
add_action( 'init', 'aft_custom_endpoint' );

Step 2: 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.

add_filter( 'woocommerce_account_menu_items', 'aft_new_menu_items' );
 
/**
* Insert the new endpoint into the My Account menu.
*
* @param array $items
* @return array
*/
function aft_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.

Step 3: 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', 'aft_endpoint_content' );
 
function aft_endpoint_content() {
    //content goes here
    echo '//content goes here';    
}

1 comment: