Action & Filter Reference
Action & Filter Reference
The Private Comments plugin provides several hooks that allow developers to programmatically alter visibility logic, extend access to specific roles, or enable/disable privacy features based on custom conditions.
Filters
wp_private_comments_user_can_view
This filter determines whether a specific user has permission to view a particular comment. Use this to implement custom access logic (e.g., allowing specific VIP members to see all discussions).
- Parameters:
$can_view(bool): The current visibility status.$comment_id(int): The ID of the comment being checked.$user_id(int): The ID of the user attempting to view the comment.
Example: Allow "Editor" roles to view private comments
add_filter('wp_private_comments_user_can_view', function($can_view, $comment_id, $user_id) {
$user = get_userdata($user_id);
if (in_array('editor', (array) $user->roles)) {
return true;
}
return $can_view;
}, 10, 3);
wp_private_comments_is_enabled
This filter controls whether the privacy logic should be applied to the current request or post. Returning false will make all comments visible as per standard WordPress behavior.
- Parameters:
$is_enabled(bool): Whether privacy is currently active.$post_id(int): The ID of the current post.
Example: Disable private comments for a specific Post Type
add_filter('wp_private_comments_is_enabled', function($is_enabled, $post_id) {
if (get_post_type($post_id) === 'public_announcement') {
return false;
}
return $is_enabled;
}, 10, 2);
wp_private_comments_allowed_roles
Allows you to extend the list of user roles that are granted "Administrator-level" visibility (the ability to see all comments across the site).
- Parameters:
$roles(array): An array of role slugs. Defaults to['administrator'].
Example: Grant "Shop Manager" access to all comments
add_filter('wp_private_comments_allowed_roles', function($roles) {
$roles[] = 'shop_manager';
return $roles;
});
Actions
wp_private_comments_before_filter_query
Fires immediately before the plugin modifies the WordPress comment query to hide restricted comments. This is useful for logging or setting up environment variables.
- Parameters:
$query(WP_Comment_Query): The comment query object being modified.
Example: Log when a filtered query is initiated
add_action('wp_private_comments_before_filter_query', function($query) {
error_log('Private comments are being filtered for query: ' . print_r($query->query_vars, true));
});
wp_private_comments_after_visibility_check
Fires after the plugin has determined the visibility of a comment.
- Parameters:
$comment_id(int): The ID of the comment.$is_visible(bool): Whether the comment was ultimately determined to be visible.
Example: Trigger an event when a private comment is hidden
add_action('wp_private_comments_after_visibility_check', function($comment_id, $is_visible) {
if (!$is_visible) {
// Custom logic for when a comment is hidden from the current user
}
}, 10, 2);