Skip to main content

WordPress Code to Automatically Delete Comments that contain URLs

PHP code to automatically delete comments with URLs in WordPress, you can use a custom PHP function.

You can automatically delete comments with URLs (a website link) as a preventive measure to mitigate potential risks associated with spam, malicious links, or inappropriate content on your blog. Here are a few reasons why a website owner might want to implement this:

SPAM comment in WordPress
An example of a SPAM comment in WordPress.
  • Spam Prevention: Many spam bots target websites with comment sections to post irrelevant or promotional comments containing URLs. Automatically deleting such comments helps maintain the integrity and quality of the comment section by removing spammy or low-quality content.
  • Security Concerns: URLs in comments can sometimes lead to malicious websites or phishing scams, posing a security risk to website visitors. By removing comments with URLs, website owners can protect their users from potentially harmful links and maintain a safe browsing environment.
  • Content Relevance: Comments containing URLs unrelated to the topic of discussion can detract from the overall user experience and the quality of the comment section. Automatically deleting such comments helps ensure that the debate remains focused and relevant to the website’s content.
  • SEO Considerations: Comments with spammy or irrelevant URLs can negatively impact the website’s search engine optimization (SEO) by associating it with low-quality or unrelated content. By removing such comments, website owners can maintain a cleaner backlink profile and improve the overall SEO performance of their website.
Automatically Delete Comments
Young child programming on the laptop.

Overall, automatically deleting comments with URLs can help WordPress website owners maintain a spam-free, secure, and user-friendly environment for their visitors while preserving the quality and relevance of the content and discussions on their website.

How to Automatically Delete Comments in WordPress

To automatically delete comments with URLs in WordPress, you can use a custom PHP function hooked to the `comment_post` action. This function will check if the comment contains a URL; if so, delete the comment immediately after it’s posted. Here’s how you can implement it:

comment_content );
      if ( strpos( $comment_content, 'http://' ) !== false || 
           strpos( $comment_content, 'https://' ) !== false) {

          wp_delete_comment( $comment_ID, true ); 
      }
    }
}
}
add_action( 'comment_post', 'timu_delete_comment_with_url', 10, 2 );

Add this code to your theme’s `functions.php` file or a custom plugin. This function will trigger every time a comment is posted (`comment_post` action). It checks if the comment content contains “http://” or “https://” indicating the presence of a URL. If a URL is found, it deletes the comment using `wp_delete_comment()` function. Remember to test this functionality thoroughly on a staging site before implementing it on your live site to ensure it works as expected. Additionally, consider informing users about your comment policy to avoid any confusion.

WordPress comments are often underused. Of course, there’s great value in increasing dwell time and building your SEO value. Still, comments can also be used for deep linking content using Pingbacks and, in some cases, even using those Pingbacks to populate your related post links. You might also be interested in my post about automatically approving Pingbacks from your domain.



About Christopher Ross

Speaks about Technology and Improving Efficiency in the Work Place

Christopher Ross is a passionate geek with diverse skills and interests, making him a dynamic and resourceful professional. With a deep-rooted enthusiasm for technology, Christopher has built a career exploring innovative solutions and advancing his knowledge in the tech field, including his love of WordPress. His journey is marked by a relentless curiosity and a commitment to continuous learning, which he applies to his professional endeavours and projects. A passable woodworker and recovering photographer, Christopher’s creative pursuits showcase his ability to balance precision and artistry. As a father and mentor, he takes pride in guiding others, fostering a spirit of curiosity and growth in those around him.

Christopher’s multifaceted background extends to his roles as a teacher, learner, sailor, and wood finisher. His dedication to education and mentorship underscores his belief in the transformative power of knowledge and technology. Christopher is keen to continue his passion for speaking to channel his skills and experiences significantly to impact the learning technology field. He aims to change the world by empowering individuals and communities with the tools and knowledge they need to thrive in a digital age. With his unique blend of technical expertise, creative talent, and unwavering commitment to lifelong learning, Christopher Ross is well-equipped to drive meaningful change and innovation in the educational technology landscape.

Education:
Currently working on my Master of Arts in Learning and Technology, Royal Roads University
Experience:
Director of Technology, Yorkville University
Website:

No Comments yet!

Your Email address will not be published.