0

I'm inserting menu items similar to the code example at https://wordpress.stackexchange.com/a/277080/137417, but I haven't encountered endpoints before and the documentation and tutorial I've looked at seem a bit beyond... Presumably, there must be a way to specify a function or something to generate the output? Or somewhere to hook into. (Or does it have to be via a template file in the theme?)

I thought maybe i should create it as a query var:

add_filter('query_vars', 'add_query_vars');
function add_query_vars($vars) {
    $vars[] = "2nd-item";
    return $vars;
}

but I'm still staring at a good old error404 when i click the link and call

...://site.com/shop/my-account/2nd-item/

Any direction much appreciated, t.i.a.

1 Answer 1

1

One needs to create an action hook 'woocommerce_account_[myendpoint]_endpoint':

add_action('woocommerce_account_' . $endpoint . '_endpoint', 'my_endpoint_content');

function my_endpoint_content() {
    //content goes here
    echo 'My content goes here';
}

http://hookr.io/actions/woocommerce_account_key_endpoint/

So, putting a few different sources together, to add a new menu item to the Woocommerce My Account dashboard one needs something like:

    <?php

add_filter('woocommerce_account_menu_items', 'add_my_menu_items');

function add_my_menu_items($items) {
    $my_items = array('2nd-item' => __('2nd Item', '[my_plugin]'),);
    $my_items = array_slice($items, 0, 1, true) +
            $my_items +
            array_slice($items, 1, count($items), true);
    return $my_items;
}

//so, for...
$endpoint = '2nd-item';

add_action('init', 'my_custom_endpoint');

function my_custom_endpoint() {
    add_rewrite_endpoint('2nd-item', EP_ROOT | EP_PAGES);
}

add_action('woocommerce_account_' . $endpoint . '_endpoint', 'my_endpoint_content');

function my_endpoint_content() {
    //content goes here
    echo 'My content goes here';
}

add_filter('query_vars', 'my_custom_query_vars', 0);

function my_custom_query_vars($vars) {
    $vars[] = '2nd-item';
    return $vars;
}

add_action('wp_loaded', 'my_custom_flush_rewrite_rules');

function my_custom_flush_rewrite_rules() {
    flush_rewrite_rules();
}
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.