7 Mar 2018

Woocommerce User Dashboard - Add Custom Menu with their Own Page | woocommerce_account_menu_items

/*
 * Step 1. Add Link to My Account menu
 */
// Note the low hook priority, this should give to your other plugins the time to add their own items...
add_filter( 'woocommerce_account_menu_items', 'add_my_menu_items', 99, 1 );
function add_my_menu_items( $items ) {
    $my_items = array(
    //  endpoint   => label
        'my_test_page' => __( 'My Test Page', 'my_coursesx' ),
        '3rd-item' => __( 'AXX3rd Item', MDL_WOODLE_PLUGIN_TOKEN ),
    );

    $my_items = array_slice( $items, 0, 1, true ) + $my_items + array_slice( $items, 1, count( $items ), true );

    return $my_items;
}


/*
 * Step 2. Register Permalink Endpoint
 */
add_action( 'init', 'test_add_endpoint' );
function test_add_endpoint() {

// WP_Rewrite is my Achilles' heel, so please do not ask me for detailed explanation
add_rewrite_endpoint( 'my_test_page', EP_PAGES );

}


/*
 * Step 3. Content for the new page in My Account, woocommerce_account_{ENDPOINT NAME}_endpoint
 */
add_action( 'woocommerce_account_my_test_page_endpoint', 'test_my_account_endpoint_content' );
function test_my_account_endpoint_content() {

// of course you can print dynamic content here, one of the most useful functions here is get_current_user_id()
echo 'Last time you logged in: yesterday from Safari.';

}


/*
 * Step 4
 */
// Go to Settings > Permalinks and just push "Save Changes" button.