Moderator
t-p
(@t-p)
These are very site-specific requirements.
You may have to develop custom solutions.
I doubt if any existing plugin can meet all your specific requirements.
Here is a free plugin directory, try searching it.
Thread Starter
elitaz
(@elitaz)
I was able to disable post author to post comment, but now he is not able to write reply any more as well….
global $current_user;
$args = array('user_id' => $current_user->ID);
if ( get_current_user_id() == $post->post_author ){
echo 'disabled';
} else {
comment_form(
array(
'logged_in_as' => null,
'title_reply' => esc_html__( 'Leave a comment', 'twentytwentyone' ),
'title_reply_before' => '<h2 id="reply-title" class="comment-reply-title">',
'title_reply_after' => '</h2>',
)
);
}
Thread Starter
elitaz
(@elitaz)
Back to my question, if someone looking to allow only one comment per user per post I think I found a solution:
add_filter('comments_open', 'restrict_users', 10, 2);
function restrict_users($open, $post_id) {
if (intval($post_id) && get_post($post_id)) {
$args = array('post_id' => $post_id, 'count' => true);
$user = wp_get_current_user();
if ($user && intval($user->ID)) { // for registered users
$skip = false;
$ignoreTheseRoles = array('administrator', 'editor'); // which user roles should be ignored
if ($user->roles && is_array($user->roles)) {
foreach ($user->roles as $role) {
if (in_array($role, $ignoreTheseRoles)) {
$skip = true;
break;
}
}
}
if (!$skip) {
$args['user_id'] = $user->ID;
$open = get_comments($args) ? false : true;
}
} else { // for guests
$commenter = wp_get_current_commenter();
if ($commenter && is_array($commenter) && isset($commenter['comment_author_email'])) {
$args['author_email'] = $commenter['comment_author_email'];
$open = get_comments($args) ? false : true;
}
}
}
return $open;
}
Now just need the way to let author to reply the comments…
There’s no way to manage replies to other comments through that filter, the data needed to decide if it’s a comment reply is not available. You need a more finessed approach. You can prevent top level comments from the post author through the ‘preprocess_comment’ filter. The ‘comment_parent’ field will be set if it’s a reply to a comment. There’s not an elegant way to prevent the comment if it doesn’t fit your criteria. All you can do is wp_die() with an appropriate message. Crude but effective.
That is only the final enforcement method anyway, the goal would be to not get to that point whenever possible. To avoid getting there, you need to alter the page’s UI for comments. If the user is the post author, use the ‘comment_form_defaults’ filter to add another class to the comment form so that it will be hidden with a related CSS rule. Should one of the “reply” links be clicked, use JavaScript or jQuery to remove that class so the form becomes visible. Have script add the class back if the cancel reply link or submit button is clicked.
Naturally a user can work around this hide and seek game and submit a top level comment by altering the CSS with a browser developer tool. If they do, then they’ll get the wp_die() message. Since they’d then be hacking your comment scheme, they don’t deserve an elegant failure anyway 🙂