admin管理员组文章数量:1287484
I created two custom button in woocommerce shop page for every products, and i'm trying to do visible this two buttons only for administrator when he/she login as an administrator.
add_action( 'woocommerce_after_shop_loop_item', 'product_visibility_button', 5 );
function product_visibility_button() {
echo '<div>';
echo '<a class="button" style="margin:10px">BTN1</a>';
echo '<a class="button" style="margin:10px">BTN1</a>';
echo '</div>';
}
I created two custom button in woocommerce shop page for every products, and i'm trying to do visible this two buttons only for administrator when he/she login as an administrator.
add_action( 'woocommerce_after_shop_loop_item', 'product_visibility_button', 5 );
function product_visibility_button() {
echo '<div>';
echo '<a class="button" style="margin:10px">BTN1</a>';
echo '<a class="button" style="margin:10px">BTN1</a>';
echo '</div>';
}
Share
Improve this question
asked Nov 12, 2021 at 11:41
Dhruv SutharDhruv Suthar
254 bronze badges
1 Answer
Reset to default 1you can use the following code to check if a user is logged in and has a valid role.
add_action( 'woocommerce_after_shop_loop_item', 'product_visibility_button', 5 );
function product_visibility_button() {
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
if ( in_array( 'administrator', (array) $user->roles ) ) {
echo '<div>';
echo '<a class="button" style="margin:10px">BTN1</a>';
echo '<a class="button" style="margin:10px">BTN1</a>';
echo '</div>';
}
}
}
本文标签: customizationShop page custom buttons which is visible to only administrator
版权声明:本文标题:customization - Shop page custom buttons which is visible to only administrator 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741223598a2361439.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论