3

I am trying to add 2 endpoints and associate them with two custom templates. 'my-server' -> 'Servers' and 'my-affiliate -> 'Affiliate'.

I also have created two custom templates:

  • my-server.php
  • my-affiliate.php

Both of them are located in my theme > woocommerce > myaccount folder. Affiliate page is pointing correctly to url/myaccount/my-affiliate.

But my problem is that Servers is giving "404 page not found" error.

I have tried to use the solution in this thread:
Assigning an endpoint to a custom template in my account pages
Ideally I should have requested this as an comment but I do not have sufficient reputation to comment. DarioFerrer 's resolution works great for a single endpoint and a single custom template.

In my case, I cannot figure out the solution for 2 or more endpoints:

  • How to include more than 2 endpoints?
  • How to assign each of them to custom templates?.

Any help will be greatly appreciated.

This is my functions.php code:

function my_custom_endpoints() {
    add_rewrite_endpoint( 'my-server', EP_ROOT | EP_PAGES );
    add_rewrite_endpoint( 'my-affiliate', EP_ROOT | EP_PAGES ); 
}
add_action( 'init', 'my_custom_endpoints' );

function my_custom_query_vars( $vars ) {
    $vars[]= 'my-server';
    $vars[] = 'my-affiliate';
    return $vars;
}

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

function my_custom_my_account_menu_items( $items ) {
    $items = array(
        'dashboard'         => __( 'Dashboard', 'woocommerce' ),
        'my-server'         => __( 'Servers', 'woocommerce' ),
        'orders'            => __( 'Orders', 'woocommerce' ),
        //'downloads'       => __( 'Downloads', 'woocommerce' ),
        //'edit-address'    => __( 'Addresses', 'woocommerce' ),
        //'payment-methods' => __( 'Payment Methods', 'woocommerce' ),
        'edit-account'      => __( 'Edit Accounts', 'woocommerce' ),
        'my-affiliate'      => __( 'Affiliate', 'woocommerce' ),
        'customer-logout'   => __( 'Logout', 'woocommerce' ),
    );

    return $items;
}

add_filter( 'woocommerce_account_menu_items', 'my_custom_my_account_menu_items' );


function my_affiliate_endpoint_content() {
    include 'woocommerce/myaccount/my-affiliate.php';   
}
add_action( 'woocommerce_account_my-affiliate_endpoint', 'my_affiliate_endpoint_content' );

function my_server_endpoint_content() {
    include 'woocommerce/myaccount/my-server.php';  
}
add_action( 'woocommerce_account_my-server_endpoint', 'my_server_endpoint_content' );

function my_custom_flush_rewrite_rules() {
    flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'my_custom_flush_rewrite_rules' );

I am using Wordpress 4.5.3 with Woocommerce 2.6.2 on Theme Cardinal (Premium theme by Swiftideas).
I am running this website on WAMP / localhost.
I am not using any affiiliate plugins. I have created both the custom templates with some general HTML content. Affiliate Tab presently does not have any Affiliate related content only just html for me to use it once every thing is set.

References:

Community
  • 1
  • 1
mesumosu
  • 155
  • 2
  • 3
  • 15
  • You should post what you've tried so far. – helgatheviking Jul 08 '16 at 12:29
  • @LoicTheAztec thanks, to give you all a background I know nothing of php, I have tried my best to resolve this. I want to add 2 custom endpoints - my-server & my-affiliate. my-affiliate is working fine, but my-server is giving an 404 error page. I have edited my questionto add what I have tried so far. – mesumosu Jul 08 '16 at 16:29
  • @LoicTheAztec , thanx a lot for the encouragement! – mesumosu Jul 08 '16 at 17:07
  • 2
    Re-save your permalinks. It's a safe bet to re-save your permalinks any time you have 404s. It can't hurt and solves a lot of problems. Presumably, you added the 2nd endpoint after switching themes because once I created some fake templates in the woocommerce folder, your code worked fine for me. Sidenote, please don't put this kind of functionality in a theme. It'd be better in a plugin and then you can flush the permalinks on activation/deactivation. – helgatheviking Jul 08 '16 at 18:43
  • 1
    @ helgatheviking , thnx a lot !!! u rock !! uwr spot on, issue got resolved. Re-saving the permalinks did it. whew! spent hours on it, can u suggest what to do now, with the question? – mesumosu Jul 08 '16 at 19:54
  • Copied my comment to a new answer. @mesumosu what do you mean "can u suggest what to do now, with the question"? The question was how to add multiple endpoints, which you are doing that correctly, you just needed to flush the permalinks. – helgatheviking Jul 08 '16 at 20:56

1 Answers1

3

Re-save your permalinks.

Any time you have 404s, It's a safe bet to re-save your permalinks. It can't hurt and solves a lot of problems. Presumably, you added the 2nd endpoint after switching themes because once I created some fake templates in the woocommerce folder, your code worked fine for me.

Sidenote

Please don't put this kind of functionality in a theme.
It'd be better in a plugin and then you can flush the permalinks on activation/deactivation.

Community
  • 1
  • 1
helgatheviking
  • 25,596
  • 11
  • 95
  • 152