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.
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! Also, Hire WooCommerce Developers for all kinds of services for web and mobile development, plugin development, mobile app, design services, and much more.
If you need custom WordPress Development services then feel free to reach us and also explore our exclusive range of WordPress WooCommerce Extensions.
Thanks for Your Time! Have a Good Day!
6 comments
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/
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?