Home>

Not to mention whether this demand is useful,After all, wordpress is for people with various needs.This function is relatively simple to implement,Just every time a user posts a comment into the database,Find out from all comments in the current post if the same username or email has already been posted,If so, skip to the error page.

Implementation code,Just put it in functions.php of the current theme (the judgment of IP is also added here, which is more secure):

//Get the comment user's ip, refer to 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 ();
 //Unlimited 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 article allows comments only once.
<a href="". Get_permalink ($commentdata ["comment_post_id"]). "">Click here to return</a>");
 }
 return $commentdata;
}
add_action ("preprocess_comment", "ludou_only_one_comment", 20);

There is no limit to the number of admin comments.Let's take a look at the method to determine whether the user is an administrator:

Determine if the user with the specified id is an administrator

This requirement is very simple to implement,Just a few lines of code,share:

function ludou_is_administrator ($user_id) {
 $user=get_userdata ($user_id);
 if (! empty ($user->roles)&&in_array ("administrator", $user->roles))
  return 1;//is the administrator
 else
  return 0;//non-administrator
}

Determine if the currently logged in user is an administrator

If it is to determine whether the currently logged in user is an administrator,You can use the following functions:

function ludou_is_administrator () {
 //wp_get_current_user function is only available in the theme's functions.php
 $currentuser=wp_get_current_user ();
 if (! empty ($currentuser->roles)&&in_array ("administrator", $currentuser->roles))
  return 1;//is the administrator
 else
  return 0;//non-administrator
}
php
  • Previous Enable PHP's pseudo-static mode
  • Next Android customize your own EditText to easily change the bottom line color
  • Trends