Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Allow post author to block user from adding a comment WordPress

  • SOLVED

I'm looking for a function that uses comment or user meta that will allow the post author to block any user from commenting on their post.

The user will only be blocked from commenting on that specific post. There needs to be a check box on the comment form saying "block this user from commenting further on this post".

Thanks

Answers (1)

2015-02-15

Reigel Gallarde answers:

What's the difference with approving comments?

Or how do you want this to happen?
are users to be blocked, registered?


Reigel Gallarde comments:

can you give me a sample scenario?

I can help you with this..


pjeaje comments:

This is very different.
1. The user is blocked from the front end hence why I asked for a check box on the comment form
2 . the user must be registered so that the comment meta can detect when that user is trying to post a comment.


Reigel Gallarde comments:

hi, can you try adding these codes in your functions.php and see if that's what you wanted...

[[LINK href="https://github.com/reigelgallarde/wpquestions.com/blob/master/block-user-comments.php"]]https://github.com/reigelgallarde/wpquestions.com/blob/master/block-user-comments.php[[/LINK]]


pjeaje comments:

The post author can't block the user, only admin.

Each post author must have the ability to block the comment user per post


Reigel Gallarde comments:

I updated the code...

[[LINK href="https://github.com/reigelgallarde/wpquestions.com/blob/master/block-user-comments.php"]]https://github.com/reigelgallarde/wpquestions.com/blob/master/block-user-comments.php[[/LINK]]

this will make post author and any admin able to block users from commenting... if you don't want any admin to interfere, you can remove current_user_can('moderate_comments') || at the top.... and that will make only post author able to block users..


pjeaje comments:

Thanks, if I can push my luck here...

1. admins can never be blocked by a user (a post author can block the admin comment)
2. a short message appears if a user has been block on the post. ("You have been blocked - You can not leave a comment on this post")
3. classes for checkbox and submit button for css styling by me.


pjeaje comments:

whoops
1. admins can never be blocked by a user (a post author can *NEVER block the admin comment)


pjeaje comments:

At the moment... the admin can be blocked


pjeaje comments:

BUG: It seems that if a user is blocked it hides the comment form from all users on all posts. including the admin.


Reigel Gallarde comments:

[[LINK href="https://github.com/reigelgallarde/wpquestions.com/blob/master/block-user-comments.php"]]https://github.com/reigelgallarde/wpquestions.com/blob/master/block-user-comments.php[[/LINK]]

code updated.

for the bug, you can comment out line 7 and line 9 to see if the checkbox is checked for all users. checkboxes must be uncheck for admin and post author. You might have checked it on my first code test.
Or, try other post and see if the same bug happen.

class of submit is submit_blk_user_comments and blk_user_comments checkbox. I also made submit button to only appear if you clicked the checkbox at least once .


pjeaje comments:

Thanks so much, your help has been brilliant.


pjeaje comments:

bug? the blocking only works if the users parent comment if un/checked, if the child comments are un/checked then it doesn't work.


pjeaje comments:

bug? the blocking only works if the users parent comment is* un/checked, if the child comments are un/checked then it doesn't work.


pjeaje comments:

This bug seems to be only on my phone?? let me check further


pjeaje comments:

OK... if it works ok but there's a little 'thing' to consider when testing it. You need to completely reload the page, not just refresh it, if that makes sense? otherwise the changes are not seen.


pjeaje comments:

By "reload" I mean put your cursor on the address url in your browser and hit enter, whereas refresh is just hitting the round recycle icon on your browser. :)