I am adding a "Conversations" endpoint so that it appears in my account in woocommerce
add_action( 'init', array($this, 'custom_endpoints') );
function custom_endpoints() {
add_rewrite_endpoint( 'conversations', EP_PERMALINK | EP_PAGES );
}
It will work just fine with a "plain" permalink structure, but with a post name structure the link in my account goes to my-account/conversations.
That says "page not found". However my-account/?conversations
does work.
What's causing this?
This is the code for the WooCommerce menu items:
add_filter( 'woocommerce_account_menu_items', array($this, 'custom_items'), 10, 1 );
function custom_items( $items ) {
$items = array_slice($items, 0, 2, true) +
array("conversations" => esc_html__( 'Conversations', 'domain' )) +
array_slice($items, 2, count($items)-2, true);
return $items;
}
I am adding a "Conversations" endpoint so that it appears in my account in woocommerce
add_action( 'init', array($this, 'custom_endpoints') );
function custom_endpoints() {
add_rewrite_endpoint( 'conversations', EP_PERMALINK | EP_PAGES );
}
It will work just fine with a "plain" permalink structure, but with a post name structure the link in my account goes to my-account/conversations.
That says "page not found". However my-account/?conversations
does work.
What's causing this?
This is the code for the WooCommerce menu items:
add_filter( 'woocommerce_account_menu_items', array($this, 'custom_items'), 10, 1 );
function custom_items( $items ) {
$items = array_slice($items, 0, 2, true) +
array("conversations" => esc_html__( 'Conversations', 'domain' )) +
array_slice($items, 2, count($items)-2, true);
return $items;
}
Share
Improve this question
asked May 8, 2020 at 4:25
Stefan SStefan S
192 bronze badges
2 Answers
Reset to default 1I would avoid using add_rewrite_endpoint()
directly to register account page endpoints in WooCommerce. WooCommerce has a filter that you can use that make sure everything else works properly, like the endpoint title filter and calls to is_wc_endpoint_url()
.
So add the endpoint with the woocommerce_get_query_vars
filter:
add_filter(
'woocommerce_get_query_vars',
function( $query_vars ) {
$query_vars['conversations'] = 'conversations';
return $query_vars;
}
);
Note that unlike the query_vars
filter, you need to use a key when adding to the array.
Now you can set the page contents with the woocommerce_account_conversations_endpoint
hook:
add_action(
'woocommerce_account_conversations_endpoint',
function() {
// Page content here.
}
);
And the title with the woocommerce_endpoint_conversations_title
filter:
add_filter(
'woocommerce_endpoint_conversations_title',
function( $title ) {
$title = 'Conversations';
return $title;
}
);
And you can continue adding it to the account menu the way you are.
I figured it out. Even if I was saving permalinks, that wasn't actually flushing the rewrite rules.
I was flushing rewrite rules on activation but I was doing it incorrectly.
If you are adding custom endpoints, you need to call the function that adds the custom endpoint in the activation file, before the flush_rewrite_rules() call.
This is in my activation function now and it works:
require_once ( PLUGIN_DIR . 'public/class-plugin-public.php' );
$publicobj = new Plugin_Public;
$publicobj->custom_endpoints();
// Flush rewrite rules
flush_rewrite_rules();
Now I'm instantiating the public class and calling the endpoints() method in the activation, before the rules flush.