Skip to content

Instantly share code, notes, and snippets.

@plugin-republic
Last active January 16, 2023 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plugin-republic/bdcc4ffef8e1168caa0790fcb33b87e7 to your computer and use it in GitHub Desktop.
Save plugin-republic/bdcc4ffef8e1168caa0790fcb33b87e7 to your computer and use it in GitHub Desktop.
<?php
/**
* Role-based check for grid view
*/
function prefix_enable_grid_for_roles( $enable, $product_id ) {
$permitted_roles = array( 'administrator' );
if( is_user_logged_in() ) {
$user = wp_get_current_user();
$roles = ( array ) $user->roles;
if( empty( array_intersect( $permitted_roles, $roles ) ) ) {
// If the current user doesn't have any of the permitted roles, present the standard view
$enable = false;
}
} else {
// User is not logged in so present standard view
$enable = false;
}
return $enable;
}
add_filter( 'wcbvp_enable_variations_grid', 'prefix_enable_grid_for_roles', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment