Last active
February 11, 2020 01:17
-
-
Save MrDOS/fd649e1d76f2885dee40eda58c70c649 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Comment Suppressor | |
* Description: Hide comments for anonymous and “Subscriber” users. | |
* Version: 1.0.1 | |
* Author: Samuel Coleman <[email protected]> | |
* 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