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
Add a comment  | 

1 Answer 1

Reset to default 1

you 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