Month: June 2022

WooCommerce Function of the Week: wc_current_user_has_role

What if you want to show hidden information only to “administrators” or “shop managers?” What about displaying a custom “My Account” tab just for logged-in customers?

Running functions based on user roles by using conditional logic is a common part of WooCommerce customization.

What if you want to show hidden information only to “administrators” or “shop managers?” What about displaying a custom “My Account” tab just for logged-in customers?

Well, wc_current_user_has_role allows us to check just that: what role group does the logged-in user belong to.

WordPress has six pre-defined roles: Super Admin, Administrator, Editor, Author, Contributor, and Subscriber. WooCommerce adds another two: Shop Manager and Customer. Other plugins may add more. You can theoretically create any number of user roles. Each role is allowed to perform a defined set of tasks called capabilities.

As usual, we’ll study the WooCommerce core function code for wc_current_user_has_role, see where and why it’s used, and finally, we’ll cover a quick case study. Enjoy!

Read more

our sponsors

A2 Hosting
Omnisend
Kinsta
Progress Planner
Elementor
WP Munich
Atarim
Patchstack