1

How can i add new endpoints to WooCommerce my account menu and show my custom content with below array? endpoint=custom_endpoint, lable=endpoint lable, icon=any icon just the below code return 404 eror

// Enable endpoint
add_filter( 'woocommerce_get_query_vars', 'myaccount_custom_endpoint_query_var' );
function myaccount_custom_endpoint_query_var( $query_vars ) {
    $query_vars['custom-endpoint'] = 'custom-endpoint';

    return $query_vars;
}

// Endpoint displayed content
add_action('woocommerce_account_custom-endpoint_endpoint', 'display_custom_endpoint_content' ); 
function display_custom_endpoint_content(){
    echo '<p>' . __("hello") . '</p>';
}

1 Answer 1

3

May be following code will be helpful. Make Sure Save Permalinks after adding this code.

/**
 * Register New Endpoint.
 *
 * @return void.
 */
function register_new_item_endpoint() {
    add_rewrite_endpoint( 'new-item', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'register_new_item_endpoint');



/**
 * Add new query var.
 *
 * @param array $vars vars.
 *
 * @return array An array of items.
 */
function new_item_query_vars( $vars ) {

    $vars[] = 'new-item';
    return $vars;
}
add_filter( 'query_vars', 'new_item_query_vars' );


/**
 * Add New tab in my account page.
 *
 * @param array $items myaccount Items.
 *
 * @return array Items including New tab.
 */
function add_new_item_tab( $items ) {

    $items['new-item'] = 'New Menu';
    return $items;
}
add_filter( 'woocommerce_account_menu_items', 'add_new_item_tab' );


/**
 * Add content to the new tab.
 *
 * @return  string.
 */
function add_new_item_content() {
    echo 'New Contents here!';
}
add_action( 'woocommerce_account_new-item_endpoint', 'add_new_item_content' );
Sign up to request clarification or add additional context in comments.

2 Comments

dear @Sanket Thakkar its work well, thanks. could you please how to do it with array? for ex. i need to add 3 custom tab: for ex: tab1 : the content of tab 1 tab2: the content of tab 2 tab3: the content of tab 3
Awesome, tried the 3 other answers for the same question on other threads and they did not work, this one worked first try. Just added it to a Snippet, no child theme, no functions.php editing, so easy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.