I have created a few custom "My Account" endpoints for Woocommerce. I am trying to limit one to being visible per user role. For the following code, I would like it to only be visible for a user with the admin role. I have tried inserting a conditional if (current_user_can('administrator'))
into my code, but haven't found a way that doesn't break the site. Any suggestions how to modify the following?
/* Create Admin Tab on Woo Account Page
------------------------------------------------------------------*/
function add_admin_tools_endpoint() {
add_rewrite_endpoint( 'admin-tools', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'add_admin_tools_endpoint' );
function add_admin_tools_query_vars( $vars ) {
$vars[] = 'admin-tools';
return $vars;
}
add_filter( 'query_vars', 'add_admin_tools_query_vars', 0 );
function add_admin_tools_link_my_account( $items ) {
$items['admnin-tools'] = 'Admin';
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'add_admin_tools_link_my_account' );
function add_admin_tools_content() {
echo "<h3 style='text-align:center;'>Administration Tools</h3> <p style='text-align:center;'>Test of various functions.</p>";
}
add_action( 'woocommerce_account_admin-tools_endpoint', 'add_admin_tools_content' );
Here is the correct way to enable and display a custom My account menu item with it's content only for a specific user role (here "administrator" user role):
Code goes in function.php file of your active child theme (or active theme). Tested and work.