How to check if a category is protected using PHP
WooCommerce Protected Categories provides a class you can use in your own code to check a category's visibility - for example, is the category private or password protected? The class is Barn2\Plugin\WC_Protected_Categories\Category_Visibility
.
To use this class, you create a new instance specifying the category ID (the term ID):
Then, use one of the methods provided to check whether it is public, protected, or private:
Each of these methods also accepts an optional $check_ancestors
flag (defaults to false
). If you set this to true
, the function will check any ancestors of the current category as well:
To check whether a protected category has been unlocked by any method (password, current user, etc), you can use the is_unlocked()
function:
To view the full API for this class, and the rest of the plugin, please refer to the plugin API documentation.