What if you need to apply a certain filter for a certain user? What could you do to achieve this? Well, there is too things we would have to look at. The user we want to work with and the filter to use.

User Capabilities

Check if user has a certain capacity or capability can be done with

current_user_can

This is for the current user as you can see. See https://developer.wordpress.org/reference/functions/current_user_can/ Or you can use

$user->has_cap( 'edit_posts' );

to focus on any user with certain capabilities.

See https://developer.wordpress.org/reference/classes/WP_User/has_cap/

Add User Roles

In our case we want to focus on the user group or type of user. So we need to look at user roles. The following code can be used to add a new role

add_role( $role, $display_name, $capabilities );

See also https://developer.wordpress.org/reference/functions/add_role/

To add a new role called company we can use the following

add_role('company', __(
   'Company'),
   array(
       'read'            => true, // Allows a user to read
       'create_posts'      => true, // Allows user to create new posts
       'edit_posts'        => true, // Allows user to edit their own posts
       'edit_others_posts' => true, // Allows user to edit others posts too
       'publish_posts' => true, // Allows the user to publish posts
       'manage_categories' => true, // Allows user to manage post categories
       )
);

Now we do not want to execute this role all the time so better to use

function imwz__update_custom_roles() { 
if ( get_option( 'custom_roles_version' ) < 1 ) { 
add_role( 'company', 'Company', 
array('read' => true, // Allows a user to read        
'create_posts'      => true, // Allows user to create new posts        
'edit_posts'        => true, // Allows user to edit their own posts        
'edit_others_posts' => true, // Allows user to edit others posts too        
'publish_posts' => true, // Allows the user to publish posts        
'manage_categories' => true, // Allows user to manage post categories        
)  ); 
update_option( 'custom_roles_version', 1 ); } } 
add_action( 'init', 'imwz__update_custom_roles' );

Now, we need to fix this code some. Too many capabilities were added here as the code was based on code for adding a moderator. So what if we only want this role to be for customers or more specifically subscribers? Then we use this code

function imwz__update_custom_roles() {
     if ( get_option( 'custom_roles_version' ) < 1 ) {         
         add_role( 'custom_role', 'Custom Subscriber', array( 'read' => true, 'level_0' => true            ) );
         update_option( 'custom_roles_version', 1 );
     }
 }
 add_action( 'init', 'imwz__update_custom_roles' );

And if we need to add two roles right away

function imwz__update_custom_roles() { 
  if ( get_option( 'custom_roles_version' ) < 2 ) { 
  add_role( 'bedrijf', 'Bedrijf', 
  array('read' => true, // Allows a user to read        
  // 'create_posts'      => true, // Allows user to create new posts        
  // 'edit_posts'        => true, // Allows user to edit their own posts        
  // 'edit_others_posts' => true, // Allows user to edit others posts too        
  // 'publish_posts' => true, // Allows the user to publish posts        
  // 'manage_categories' => true, // Allows user to manage post categories        
  )  ); 
  add_role( 'particulier', 'Particulier', 
  array('read' => true, // Allows a user to read        
  // 'create_posts'      => true, // Allows user to create new posts        
  // 'edit_posts'        => true, // Allows user to edit their own posts        
  // 'edit_others_posts' => true, // Allows user to edit others posts too        
  // 'publish_posts' => true, // Allows the user to publish posts        
  // 'manage_categories' => true, // Allows user to manage post categories        
  )  ); 
  update_option( 'custom_roles_version', 2 ); } } 
  add_action( 'init', 'imwz__update_custom_roles' );

Props https://profiles.wordpress.org/rogertheriault/

mu-plugins loads too early, so use an action hook (like ‘init’) to wrap your add_role() call if you’re doing this in the context of an mu-plugin.

If Role Company Use Minimum 10 Items

You can apply this newly created role using any added filter really. We will use one we needed for a customer in a WooCommerce surrounding, but you can use yours.

We can use ad_filters to use a filter on the minimum product quantity set by the plugin Product Quantity for WordPress using this code:

add_filter( 'alg_wc_pq_get_product_qty_min', 'my_product_qty_min_by_user_role', 10, 2 );
function my_product_qty_min_by_user_role( $qty, $product_id ) {
  $current_user = wp_get_current_user();
  return ( in_array( 'bedrijf', $current_user->roles ) ? 10 : $qty );
}

See plugin class class-alg-wc-pq-core.php and http://hookr.io/filters/woocommerce_quantity_input_min/

The filter used here is a WooCommerce filter that is also used in the mentioned plugin. We simply use the filter if the user has the role company. You can also easily use another conditional of your own.

Recommended Posts

No comment yet, add your voice below!


Add a Comment

Your email address will not be published. Required fields are marked *