: This article mainly introduces WordPress to restrict non-administrator users to comment only once after the article. if you are interested in PHP tutorials, refer to it. 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}
The above section describes the restrictions that non-administrator users can only comment on WordPress once after the article, including some content, and hope to help friends who are interested in PHP tutorials.