Sunday, March 24, 2019

PHP script to remove links from WordPress comments

Today I was doing some cleanup on the blog and I discovered that many of the older comments had what could be considered spam links inside the body of the comment itself.

Instead of manually going through each one, which would take a lot of time, I decided to create a small PHP script to do this task automatically for me. Below you will find the script and an explanation of what it does.

<?php

$db = mysqli_connect('localhost','database_user','password','database_name')
or die('Error connecting to MySQL server.');

$query = "SELECT comment_ID,comment_content FROM wp_comments";

$result = mysqli_query($db,$query)
or die('Error querying database.');

while($row = mysqli_fetch_array($result)){
$id = $row[0];
$content =strip_tags($row[1]);

$regex = "@(https?://([-w.]+[-w])+(:d+)?(/([w/_.#-]*(?S+)?[^.s])?).*$)@";
$content = preg_replace($regex, ' ', $content);

//echo $content. "<br>";

$stmt = mysqli_prepare($db,"UPDATE wp_comments SET comment_content=? WHERE comment_ID = ?");
mysqli_stmt_bind_param($stmt,"si",$content, $id);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
}

?>

How to use the script above:
1. Copy and paste the script into a text file and save it with the PHP extension, for example you can call with comments.php
2. Update the database connection with your user and database credentials
3. Upload the file to your website
4. Run the file by accessing it using your browser

That’s it. The script will run through all your comments removing all links (but preserving the text of the links).

Original post: PHP script to remove links from WordPress comments


This post is courtesy of: https://www.dailyblogtips.com

10 comments:


  1. i was browsing the web and stubbled across your post. great post! i love it and i notice that your link to ahref.com/blogkeyword-reseach.
    this article inspired me to write an even better articles , and by better i mean that i made it longer and i rhing it might make a good fit for your page
    Contact Us
    Address:
    Phone: +1 (903) 326-9722
    Email: kushkhali28@gmail.com

    The Best Online Marijuana Dispensary with Shipping Worldwide
    https://www.khaliplug420.com/about-us
    khaliplug420: Comes with farm to flame technology based cultivation, every strains and bud of khaliplug420 Dispensary consists of premium quality aroma, flavor, and consistency. We have shipping ports in the UK, US, Europe, and Australia. Order online at great prices.
    The Best Online Marijuana Dispensary with Shipping Worldwide
    https://www.khaliplug420.com/about-us
    khaliplug420: Comes with farm to flame technology based cultivation, every strains and bud of khaliplug420 Dispensary consists of premium quality aroma, flavor, and consistency. We have shipping ports in the UK, US, Europe, and Australia. Order online at great prices.

    Buy Medical Marijuana Online in USA
    https://www.khaliplug420.com/product-category/marijuana-edibles/
    khaliplug420: If you are looking for buy medical marijuana online in USA. Welcome to Khali plug 420. Here you get medical marijuana at affordable price. Buy medical marijuana online today with free shipping.

    CANNABIS OIL- Order Online CBD Oil Canada, US, UK, Europe, Aust
    https://www.khaliplug420.com/product-category/cannabis-oil/
    khaliplug420: Order premium quality Cannabis Oil, cbd oil cartridge, edible cannabis oil, lackberry kush, bubblegum, phoenix tears, sour diesel, super lemon haze.

    Natural Cannabis Seeds USA - Cross Pollination, Best Offspring
    https://www.khaliplug420.com/product-category/cannabis-seeds/
    khaliplug420: Wants to buy natural cannabis Seeds in USA. Visit once khaliplug420.com. Here you get best quality natural cannabis Seeds at best price.

    Dispensary Near Me Recreational - Get Sunrocks and Moonrocks
    https://www.khaliplug420.com/product/sunrocks/
    khaliplug420: Looking for trusted cannabis dispensary near me / you then visit khaliplug420 here you get best quality sunrocks and moonrocks: 60 - 80% THC, hash oil etc.


    Experience Eternal Pleasure with Organic Indica Cannabis Strains in the USA at Khali plug 420
    https://www.khaliplug420.com/product-category/buds/indica/
    khaliplug420: Experience Eternal Pleasure with Organic Indica Cannabis Strains in the USA at khaliplug420 dispensary provides online delivery of organically growth Indica Cannabis Strains usa at a competitive market price.

    ReplyDelete
  2. Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include.

    seo services in lahore

    ReplyDelete
  3. When you use a genuine service, you will be able to provide instructions, share materials and choose the formatting style.

    seo company in lahore

    ReplyDelete
  4. I am impressed. I don't think Ive met anyone who knows as much about this subject as you do. You are truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. Really, great blog you have got here.
    Best SEO Services in Lahore

    ReplyDelete
  5. Excellent to be visiting your blog again, it has been months for me. Rightly, this article that I've been served for therefore long. I want this article to finish my assignment within the faculty, and it has the same topic together with your article. Thanks for the ton of valuable help, nice share.
    Best Web Development Company in Lahore

    ReplyDelete
  6. Hey, I am so thrilled I found your blog, I am here now and could just like to say thank for a tremendous post and all round interesting website. Please do keep up the great work. I cannot be without visiting your blog again and again.
    SEO Services

    ReplyDelete
  7. I read your blog frequently, and I just thought I’d say keep up the fantastic work! It is one of the most outstanding blogs in my opinion.
    Digital Marketing Company in Lahore

    ReplyDelete
  8. DigisolHub
    SEO and PPC management are two terms that are often used interchangeably, but they have different meanings. SEO (search engine optimization) is the process of making content on your website more accessible to search engines by changing its code so it can be found better…. read more

    ReplyDelete