I have the following code in the functions.php file.
add_action( 'init', 'add_admin_tools_account_endpoint' );
function add_admin_tools_account_endpoint() {
add_rewrite_endpoint( 'wp-admin', EP_PAGES );
}
add_filter ( 'woocommerce_account_menu_items', 'custom_account_menu_items', 10 );
function custom_account_menu_items( $menu_links ){
if ( current_user_can('administrator') ) {
$menu_links = array_slice( $menu_links, 0,0 , true )
+ array( 'wp-admin' => __('Admin tools') )
+ array_slice( $menu_links, 0, NULL, true );
}
return $menu_links;
}
add_action( 'woocommerce_account_admin-tools_endpoint', 'admin_tools_account_endpoint_content' );
function admin_tools_account_endpoint_content() {
if ( current_user_can('administrator') ) {
echo "<h3 class='headline'>Admin Tools</h3>
<p style='text-align:center;'>Test of various functions.</p>";
}
}
My problem is that it points to: https://{PATH}/my-account/wp-admin
. I need to go to https://{PATH}/wp-admin
instead.
2
Answers
You should remove the endpoint from the my-account page using the remove_rewrite_endpoint function. Then, in the custom_account_menu_items function, we add a direct link to the wp-admin page using the admin_url function.
Try this..
In the add_admin_tools_account_endpoint() function, we have changed the EP_PAGES argument to EP_ROOT. This sets the endpoint to the root URL instead of to the account page
I have added a new function admin_tools_account_endpoint_redirect() that redirects the endpoint to site_url( ‘/wp-admin/’ ) if the wp-admin query var is present in the URL. This is necessary because we have changed the endpoint to the root URL, so we need to manually redirect it to the correct page
We have changed the action in the add_action() call for displaying the endpoint content to woocommerce_account_wp-admin_endpoint to match the new endpoint name.
With these changes, the URL for the "Admin tools" link in the account menu should point to https://{PATH}/wp-admin, as desired