WordPress restricted Non-administrator Users can only comment once after the article _php skills

Source: Internet
Author: User
Tags comments

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
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.