Skip to content

Instantly share code, notes, and snippets.

@MrDOS
Last active February 11, 2020 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrDOS/fd649e1d76f2885dee40eda58c70c649 to your computer and use it in GitHub Desktop.
Save MrDOS/fd649e1d76f2885dee40eda58c70c649 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Comment Suppressor
* Description: Hide comments for anonymous and “Subscriber” users.
* Version: 1.0.1
* Author: Samuel Coleman <samuel@seenet.ca>
* License: WTFPLv2
*/
/**
* Suppress comments for a post if the user is anonymous, or if the user only
* belongs to the “Subscriber” role.
*
* @param array $comments the comments for the post
* @param int $post_id the ID of the post
*/
function suppress_comments($comments, $post_id)
{
if (!is_user_logged_in())
{
return [];
}
$user = wp_get_current_user();
$roles = $user->roles;
/* The user should never have zero roles, but if they do, they're
* _definitely_ not allowed to see comments. Otherwise, users may have
* multiple roles; e.g., “Contributor” stacks on top of “Subscriber”, so we
* only want to suppress comments for users who have _only_ the
* “Subscriber” role. */
if (count($roles) === 0
|| (count($roles) === 1 && in_array('subscriber', $roles)))
{
return [];
}
return $comments;
}
add_filter('comments_array', 'suppress_comments', 0, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment