Customizing Visibility Rules
Customizing Visibility Rules
By default, the Private Comments plugin restricts comment visibility to the comment author, the post author, and site administrators. Developers can extend or modify these rules using the following hooks.
Modifying Allowed Roles
If you want to allow additional user roles (e.g., "Editor" or "Moderator") to view all comments regardless of authorship, use the wppc_allowed_roles filter.
Example: Adding the Editor role to the whitelist
/**
* Add 'editor' to the list of roles allowed to view all private comments.
*
* @param array $roles Array of capability strings or role names.
* @return array Modified array of roles.
*/
add_filter('wppc_allowed_roles', function($roles) {
$roles[] = 'editor';
return $roles;
});
Excluding Specific Post Types
You may want the privacy logic to apply only to standard posts while keeping comments public on other post types, such as "Products" or "Pages." Use the wppc_excluded_post_types filter to define these exceptions.
Example: Disabling private comments for the 'product' post type
/**
* Exclude certain post types from privacy restrictions.
*
* @param array $post_types Array of post type slugs to exclude.
* @return array Modified array of post types.
*/
add_filter('wppc_excluded_post_types', function($post_types) {
$post_types[] = 'product';
$post_types[] = 'page';
return $post_types;
});
Conditional Bypass
For more granular control, use the wppc_is_private_enabled filter. This allows you to programmatically enable or disable the plugin's privacy logic based on custom logic (e.g., specific categories, time of day, or post IDs).
Example: Disabling privacy for a specific Category
/**
* Conditionally disable comment privacy logic.
*
* @param bool $is_enabled Whether the privacy restriction is active.
* @param int $post_id The ID of the current post.
* @return bool
*/
add_filter('wppc_is_private_enabled', function($is_enabled, $post_id) {
// Disable privacy if the post is in the 'public-discussion' category
if (has_term('public-discussion', 'category', $post_id)) {
return false;
}
return $is_enabled;
}, 10, 2);
Usage Notes
- Scope: These filters affect both the front-end comment display and the
get_commentsquery results. - Priority: If you are using these filters within a theme, ensure they are added to your
functions.phpfile. If you are using them in a custom plugin, ensure your plugin loads after Private Comments.