Table of Contents
WordPress restricts non-administrator users to only comment once after an article. WordPress
Articles you may be interested in:
Home Backend Development PHP Tutorial WordPress restricts non-admin users to comment only once after an article, wordpress_PHP tutorial

WordPress restricts non-admin users to comment only once after an article, wordpress_PHP tutorial

Jul 12, 2016 am 09:01 AM
wordpress Comment

WordPress restricts non-administrator users to only comment once after an article. WordPress

Some netizens have previously asked whether there is a way in WordPress to only allow users to comment on each article. once?

Let’s not say whether this requirement is useful or not. After all, WordPress is for people with various needs. This function is relatively simple to implement. You only need to search all the comments on the current article to see if the same user name or email address has already posted a comment. If so, jump to the error page. .

To implement the code, just put it in the functions.php of the current theme (the IP judgment is also added here, which is safer):

// 获取评论用户的ip,参考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();
 
 // 不限制管理员发表评论
 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('本站每篇文章只允许评论一次。<a href="'.get_permalink($commentdata['comment_post_ID']).'">点此返回</a>');
 }
 
 return $commentdata;
}
add_action( 'preprocess_comment' , 'ludou_only_one_comment', 20);

Copy after login

There is no limit on the number of comments an administrator can make, so let’s take a look at how to determine whether a user is an administrator:

Determine whether the user with the specified ID is an administrator

This requirement is very simple to implement. It can be done with just a few lines of code. Let me share it:

function ludou_is_administrator($user_id) {
 $user = get_userdata($user_id);
 if(!empty($user->roles) && in_array('administrator', $user->roles))
  return 1; // 是管理员
 else
  return 0; // 非管理员
}

Copy after login

Determine whether the currently logged in user is an administrator

If you want to determine whether the currently logged in user is an administrator, you can use the following function:

function ludou_is_administrator() {
 // wp_get_current_user函数仅限在主题的functions.php中使用
 $currentUser = wp_get_current_user();

 if(!empty($currentUser->roles) && in_array('administrator', $currentUser->roles)) 
  return 1; // 是管理员
 else
  return 0; // 非管理员
}
Copy after login

Articles you may be interested in:

  • Detailed explanation of relevant PHP functions for creating and adding filters in WordPress
  • PHP functions for creating and obtaining sidebars in WordPress Explain
  • PHP function implementation of adding categories and tags to media files in WordPress

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1087280.htmlTechArticleWordPress restricts non-administrator users to only comment once after an article. Some netizens have proposed before that in WordPress Is there a way to only allow users to comment once per article? ...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to display wordpress comments How to display wordpress comments Apr 20, 2025 pm 12:06 PM

Enable comments in WordPress website: 1. Log in to the admin panel, go to "Settings" - "Discussions", and check "Allow comments"; 2. Select a location to display comments; 3. Customize comments; 4. Manage comments, approve, reject or delete; 5. Use &lt;?php comments_template(); ?&gt; tags to display comments; 6. Enable nested comments; 7. Adjust comment shape; 8. Use plugins and verification codes to prevent spam comments; 9. Encourage users to use Gravatar avatar; 10. Create comments to refer to

WordPress website is online but cannot be searched WordPress website is online but cannot be searched Apr 20, 2025 am 09:00 AM

Reasons why WordPress websites cannot be found in search engines: 1. Indexing issues; 2. Content issues; 3. Website technical issues; 4. Link issues; 5. Other issues such as geographical restrictions, website name and social media presence.

How to close comments with wordpress How to close comments with wordpress Apr 20, 2025 am 11:54 AM

How to turn off a comment in WordPress? Specific article or page: Uncheck Allow comments under Discussion in the editor. Whole website: Uncheck "Allow comments" in "Settings" -> "Discussion". Using plug-ins: Install plug-ins such as Disable Comments to disable comments. Edit the topic file: Remove the comment form by editing the comments.php file. Custom code: Use the add_filter() function to disable comments.

How to copy wordpress code How to copy wordpress code Apr 20, 2025 pm 12:00 PM

How to copy WordPress code? Copy from the admin interface: Log in to the WordPress website, navigate to the destination, select the code and press Ctrl C (Windows)/Command C (Mac) to copy the code. Copy from a file: Connect to the server using SSH or FTP, navigate to the theme or plug-in file, select the code and press Ctrl C (Windows)/Command C (Mac) to copy the code.

How to upload source code for wordpress How to upload source code for wordpress Apr 20, 2025 pm 12:03 PM

You can install the FTP plug-in through WordPress, configure the FTP connection, and then upload the source code using the file manager. The steps include: installing the FTP plug-in, configuring the connection, browsing the upload location, uploading files, and checking that the upload is successful.

WordPress website building and avoid pits WordPress website building and avoid pits Apr 20, 2025 am 08:06 AM

Be cautious when building a WordPress website. The guide to breaking through pits helps you avoid risks: choose paid themes and avoid the quality and safety risks of free themes. "Less is more" when installing plugins to avoid website speed and compatibility issues. Regularly optimize the database to ensure the smooth operation of the website. Pay attention to security measures and regularly update and install security plug-ins. Modify the code carefully to avoid website crashes and do it in a test environment if necessary. Pay attention to performance optimization, improve website speed, and improve user experience.

WordPress website account login WordPress website account login Apr 20, 2025 am 09:06 AM

To log in to a WordPress website account: Visit the login page: Enter the website URL plus "/wp-login.php". Enter your username and password. Click "Login". Verification Two-step Verification (optional). After successfully logging in, you will see the website dashboard.

What to do if there is an error in wordpress What to do if there is an error in wordpress Apr 20, 2025 am 11:57 AM

WordPress Error Resolution Guide: 500 Internal Server Error: Disable the plug-in or check the server error log. 404 Page not found: Check permalink and make sure the page link is correct. White Screen of Death: Increase the server PHP memory limit. Database connection error: Check the database server status and WordPress configuration. Other tips: enable debug mode, check error logs, and seek support. Prevent errors: regularly update WordPress, install only necessary plugins, regularly back up your website, and optimize website performance.

See all articles