WordPress restricts non-administrator users to comment only once after the article. wordpress_PHP tutorial

Source: Internet
Author: User
WordPress restricts non-administrator users to comment only once after the article. WordPress restricts non-administrator users to comment only once after the article. some netizens have suggested that in wordpress, is there a way to allow users to comment only once in each article? WordPress restricts non-administrator users to comment only once after the article.

Some netizens suggested that in WordPress, is there a way to allow users to comment only once in each article?

I am not sure whether this requirement is useful. after all, WordPress is intended for users with various needs. This function is easy to implement. you only need to find out whether the same user name or email address has been commented on from all the comments in the current article before the user comments are added to the database, if yes, you can jump to the error page.

The implementation code is put in the functions. php of the current topic (the IP judgment is also added here, which is more secure ):

// Obtain the ip address of the comment User. For more information, see wp-shortdes/comment. phpfunction 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 restrict the administrator from posting comments if (empty ($ currentUser-> roles) |! In_array ('admin', $ 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 (' each article on this site can only be commented once. Click here to return ');} return $ commentdata;} add_action ('preprocess _ comment', 'ludou _ only_one_comment', 20 );

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

Determines whether the user with the specified id is an administrator.

The implementation of this requirement is very simple. let's take a few lines of code and share the following:

Function ludou_is_administrator ($ user_id) {$ user = get_userdata ($ user_id); if (! Empty ($ user-> roles) & in_array ('admin', $ user-> roles) return 1; // is the administrator else return 0; // Non-administrator}

Determine whether the current logon user is an administrator

To determine whether the current logon user is an administrator, use the following function:

Function ludou_is_administrator () {// The wp_get_current_user function can only be used in the topic's functions. php $ currentUser = wp_get_current_user (); if (! Empty ($ currentUser-> roles) & in_array ('admin', $ currentUser-> roles) return 1; // is the administrator else return 0; // Non-administrator}

Articles you may be interested in:
  • Describes the PHP functions used to create and add filters in WordPress.
  • PHP functions used to create and retrieve sidebar in WordPress
  • PHP function for adding categories and tags to media files in WordPress

Some netizens have suggested that in WordPress, is there a way to make every article only allow users to comment once ?...

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.