WordPress Limit Non-admin users in the post can only comment once, WordPress
Before a netizen put forward, 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 each time the user published comments into the database, from all comments in the current article to find out whether the same user name or mailbox has been commented, if any, jump to the error page.
Implement the code and put it in the functions.php of the current topic (it also adds a judgment on the IP, more insurance):
Get the IP of the comment user, refer to wp-includes/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 admin 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. Click here to return '); } return $commentdata;} Add_action (' preprocess_comment ', ' ludou_only_one_comment ', 20);
There is no limit to the number of admin comments, so let's take a look at the method of judging whether the user is an administrator:
The user who determines the specified ID is not an administrator
This requirement is very simple to implement, a few lines of code to get it 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-administrator}
Determine if the current logged on user is an administrator
If you are judging 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 only used in functions.php of the subject $currentUser = Wp_get_current_user (); if (!empty ($currentUser->roles) && in_array (' Administrator ', $currentUser->roles)) return 1; is admin else return 0;//non-administrator}
Articles you may be interested in:
- Detailed PHP functions for creating and adding filters in WordPress
- PHP functions for creating and getting sidebar in WordPress
- PHP functionality for adding categories and tags to media files in WordPress
http://www.bkjia.com/PHPjc/1087280.html www.bkjia.com true http://www.bkjia.com/PHPjc/1087280.html techarticle WordPress Limit Non-admin users in the post can only be commented once, WordPress before a netizen, in WordPress there is no way to achieve each article only allow users to comment once? ...