Before a netizen proposed, in WordPress there is no way to achieve each article only allow users to comment once?
Temporarily do not say this demand has no use, after all, WordPress is for people with a variety of needs. This feature is also relatively simple to implement, just every time the user published comments into the database, from the current article in all the comments to find whether the same user name or mailbox has been commented, if there is to jump to the wrong page.
Implement the code, put it in the functions.php of the current topic (this also adds to the IP judgment, more insurance):
Get comments on the user's IP, reference wp-includes/comment.php
function Ludou_getip () {
$ip = $_server[' remote_addr '];
$ip = Preg_replace ('/[^0-9a-fa-f:.,]/', ', $ip);
return $ip;
}
function Ludou_only_one_comment ($commentdata) {
global $wpdb;
$currentUser = Wp_get_current_user ();
Do not limit administrator comments
if (empty ($currentUser->roles) | |!in_array (' Administrator ', $currentUser->roles)) {
$ bool = $wpdb->get_var ("Select comment_id from $wpdb->comments WHERE comment_post_id =". $commentdata [' Comment_ post_id ']. " and (Comment_author = ' ". $commentdata [' Comment_author ']." ' OR comment_author_email = ' ". $commentdata [' Comment_author_email ']." ' OR comment_author_ip = ' ". Ludou_getip ()." ') LIMIT 0, 1; ");
if ($bool)
Wp_die (' This site is only allowed to comment once per article. <a href= "'. Get_permalink ($commentdata [' comment_post_id '])." > Point this return </a> ');
return $commentdata;
}
Add_action (' preprocess_comment ', ' ludou_only_one_comment ', 20);
There is no limit to the number of comments that administrators have, so let's take a look at how to determine whether a user is an administrator:
Determine if the user of the specified ID is an administrator
This requirement is very simple to implement, a few lines of code to be done, share:
function Ludou_is_administrator ($user _id) {
$user = get_userdata ($user _id);
if (!empty ($user->roles) && in_array (' Administrator ', $user->roles)) return
1;//IS admin
else return
0;//non admin
}
Determine if the current logged-on user is an administrator
If you are determining whether the current logged-on user is an administrator, you can use the following function:
function Ludou_is_administrator () {
//Wp_get_current_user functions are used only in the functions.php of the theme
$currentUser = wp_get_ Current_User ();
if (!empty ($currentUser->roles) && in_array (' Administrator ', $currentUser->roles)) return
1; Is admin
else return
0;//non-admin
}