remove columns from woocommerce product page

2019-06-09 08:35发布

Hi I am trying to limit the options certain users have when viewing the products list within woocommerce for wordpress admin pages.

if( current_user_can('vendor') ) {
function my_columns_filter( $columns ) {

    unset($columns['tags']);
    unset($columns['featured']);
    unset($columns['type']);
    return $columns;
}
}
add_filter( 'manage_edit-product_columns', 'my_columns_filter', 10, 1 );

Any help or guidance would be much appreciated.

2条回答
叛逆
2楼-- · 2019-06-09 08:58

You are not using the correct column names, or perhaps WC has changed them since you posted your question. It also better to check if a column still exists before removing it in case WC does change their column names.

Here is a future proof solution you can past into your theme's functions.php:

function my_product_columns_filter( $columns ) {
    if ( current_user_can( 'vendor' ) ) {
        foreach ( array( 'product_tag', 'featured', 'product_type' ) as $name ) {
            if ( isset( $columns[ $name ] ) ) {
                unset( $columns[ $name ] );
            }
        }
    }

    return $columns;
}

add_filter( 'manage_edit-product_columns', 'my_product_columns_filter' );

no coding solution

If you're just looking for a quick solution without the need for coding, you could use the free admin columns plugin from wordpress.org, which allows you to add and remove columns with a few clicks.

查看更多
欢心
3楼-- · 2019-06-09 09:04

You're doing it wrong.

Place if/else inside the function instead of wrapping the function.

function my_columns_filter( $columns ) {
    if( current_user_can('vendor') ) {
        unset($columns['tags']);
        unset($columns['featured']);
        unset($columns['type']);

        return $columns;
    }
}
add_filter( 'manage_edit-product_columns', 'my_columns_filter', 10, 1 );
查看更多
登录 后发表回答