$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - How to call code when adding WooCommerce menu items via woocommerce_account_menu_items|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugin development - How to call code when adding WooCommerce menu items via woocommerce_account_menu_items

matteradmin9PV0评论

I'm inserting menu items similar to the code example at , 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/shop/my-account/2nd-item/

Any direction much appreciated, t.i.a.

I'm inserting menu items similar to the code example at https://wordpress.stackexchange/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/shop/my-account/2nd-item/

Any direction much appreciated, t.i.a.

Share Improve this question asked Dec 20, 2018 at 23:02 Andre ClementsAndre Clements 1347 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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();
}
Post a comment

comment list (0)

  1. No comments so far