1. Knowledge Base
  2. WooCommerce Protected Categories
  3. Developer Documentation

Hooks and filters

WooCommerce Protected Categories contains a number of hooks which you can use to customize the plugin’s behavior.

Please note that this code is aimed at developers and if you don’t know how to use it then you should ask your developer. If you don’t have one, we recommend posting a job on Codeable. We have partnered with them to provide plugin customizations for our customers.

Need professional assistance?

We've partnered with Codeable to provide our customers with expert help if required.

Actions

wc_ppc_before_password_form

Action fired before the category password form. You might use this to wrap the password form with extra HTML markup.

add_action( 'wc_ppc_before_password_form', function() {
    echo '<div class="container col-12">';
} );

wc_ppc_after_password_form

Action fired take after the category password form.

add_action( 'wc_ppc_after_password_form', function() {
    echo '</div>';
}

wc_ppc_protected_woocommerce_page

Action fired when the current page is a protected category or product. This action is fired during the wp hook, before the template has been loaded.

add_action( 'wc_ppc_protected_woocommerce_page', function() {
    global $wp_query;
    // do something the query.
}

wc_ppc_handle_password_protection

Action fired when the current page is a password protected category or product. This action is fired during the wp hook.

wc_ppc_handle_user_protection

Action fired when the current page is a user protected, role protected, or private category or product.

wc_ppc_handle_protected_shop_page

Action fired when the main Shop page in WooCommerce is the same as your ‘Password entry page’ set under WooCommerce → Settings → Products → Protected categories. In this case, the main shop page displays the password entry form.

wc_ppc_prevent_caching

Action fired if the current page is protected. Use this hook if you want to do extra things to prevent caching on your site. We currently set the following cache constants to true when the page is protected: DONOTCACHEPAGE, DONOTCACHEOBJECT and DONOTCACHEDB

wc_ppc_after_password_form

Action  fired take after the category password form.

add_action( 'wc_ppc_after_password_form', function() {
    echo '</div>';
}

wc_ppc_after_password_form

Action  fired take after the category password form.

add_action( 'wc_ppc_after_password_form', function() {
    echo '</div>';
}

Filters

wc_ppc_current_user_allowed_by_role

Filter which allows you to override whether the current user is allowed access when a category is role protected. Returns a boolean.

add_filter( 'wc_ppc_current_user_allowed_by_role', function( $allowed_access, $category_id ) { 
    // do something with $allowed_access
    return $allowed_access; 
}

wc_ppc_current_user_allowed_by_id

Filter which allows you to override whether the current user is allowed access when a category is user protected. Returns a boolean.

add_filter( 'wc_ppc_current_user_allowed_by_id', function( $allowed_access, $category_id ) { 
    // do something with $allowed_access
    return $allowed_access; 
}

wc_ppc_current_user_allowed_private_access

Filter which allows you to override whether the current user is allowed access when a category is private. Returns a boolean.

add_filter( 'wc_ppc_current_user_allowed_private_access', function( $allowed_access, $category_id ) { 
    // do something with $allowed_access
    return $allowed_access; 
}

wc_ppc_password_form_container_class

Filter which allows you to override the CSS class for the container for the password entry form. This is applied to a <div> element which surrounds the form. Returns a string.

wc_ppc_password_form_class

Filter which allows you to override class for the password entry form current user is allowed access when a category is private. Returns a string. Defaults to: wc-ppc-password-form post-password-form category-login

wc_ppc_password_form

Filter which allows you to modify the HTML for the password form before it’s displayed. Returns a string.

add_filter( 'wc_ppc_password_form', function( $form ) {
    // do something with $form
    return $form;
}

wc_ppc_password_form_heading

Filter which allows you to modify the heading used for the password form. Returns a string.

wc_ppc_password_form_message

Filter which allows you to modify the password form message, displayed above the password input box. Returns a string.

wc_ppc_user_protected_login_url

Filter which allows you to override the URL used for the WordPress login page when the current page is role protected, user protected, or private. This only applies when the ‘Show WordPress login page’ option is selected for the When logged out option in the plugin settings.

wc_ppc_product_id_for_protection_check

When a product is checked for protection (for example, when viewing a product page), we use its product ID. This filter allows you to override the ID used. Returns an integer. Takes two parameters – the product ID and the current product object.

wc_ppc_category_protection_priority_order

Filter which allows you to override the priority order used for the different category protection types. Items earlier in the array take higher priority than those that come after. Should return an array. Defaults to: array( 'password', 'private', 'user', 'role' )

wc_ppc_password_expires

Filter which allows you to modify the expiration time for the password cookie; i.e. how long the user will be able to access the protected category before they need to re-enter their password. You can set this via the plugin options (WooCommerce → Settings → Products → Protected categories), but if you want more fine-grained control over the expiry time, use this filter.

The return value should be Unix timestamp in seconds since the epoch. The easiest way to set this is to use the time() function and add the number of seconds you want to it. For example, to expire the cookie after 8 hours:

add_filter( 'wc_ppc_password_expires',function( $expires ) {
    return time() + 8 * HOUR_IN_SECONDS;
}

wc_ppc_admin_available_roles

Filter which allows you to modify the list of available user roles displayed in the admin when creating or editing a protected category. Returns an array of arrays in the form ‘role_name’ => array(). Defaults to wp_roles()->roles.

wc_ppc_admin_user_select_args

Filter which allows you to modify the args used when selecting users to display in the user list when creating or editing a protected category. Returns and array. Defaults to:

array(
    'blog_id' => get_current_blog_id(),
    'orderby' => 'display_name',
    'order' => 'ASC',
    'fields' => array( 'ID', 'user_login', 'display_name' )
);

wc_ppc_admin_available_users

Filter which allows you to modify the list of users displayed when creating or editing a protected category in the admin. Returns an array of WP_User objects.

Still need help?

If searching the knowledge base hasn't answered your question, please contact support.

Related Articles