为 Woocommerce 中的特定用户角色自定义我的帐户新菜单项 [英] Custom my account new menu item for a specific user role in Woocommerce

查看:39
本文介绍了为 Woocommerce 中的特定用户角色自定义我的帐户新菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为 Woocommerce 创建了一些自定义的我的帐户"端点.我试图将一个限制为每个用户角色可见.对于以下代码,我希望它仅对具有 admin 角色的用户可见.我试过在我的代码中插入一个条件 if (current_user_can('administrator')) ,但还没有找到一种不会破坏网站的方法.任何关于如何修改以下内容的建议?

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>&nbsp;<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):

add_action( 'init', 'add_admin_tools_account_endpoint' );
function add_admin_tools_account_endpoint() {
    add_rewrite_endpoint( 'admin-tools', 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,3 , true )
        + array( 'admin-tools' => __('Admin tools') )
        + array_slice( $menu_links, 3, 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 style='text-align:center;'>Administration Tools</h3>
        <p style='text-align:center;'>Test of various functions.</p>";
    }
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经过测试和工作.

Code goes in function.php file of your active child theme (or active theme). Tested and work.

您需要刷新重写规则:在 Wordpress 管理中的设置">永久链接"下,只需单击保存更改"按钮一次.你完成了.

You will need to refresh rewrite rules: In Wordpress admin under "Settings" > Permalinks, just click on "Save changes" button once. You are done.

这篇关于为 Woocommerce 中的特定用户角色自定义我的帐户新菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆