Tag Archives: WordPress

Delete Comment Spam using a Cron Job

Delete Comment Spam using a Cron Job

WordPress catches all of the comment spam, but it doesn’t seem to delete it often enough. If you have a high traffic site, this can become megabytes in your database. Bleh. I don’t have a high traffic site, but I sync my databases off-site everyday and spam wastes my bandwidth. So I delete all the comments marked as spam automatically each day using a scheduled cron job so that I never even have to see it. Could I possibly delete a real comment? Yes. Does the convenience of never even knowing about comment spam outweigh the risk? You bet.

Create a bash script in your scripts folder by typing this at the command prompt:

sudo pico /home/brockangelo/scripts/del_spam.sh

sudo: elevate privileges
pico: use your editor of choice
/home…/scripts/: wherever you keep all your bash scripts
del_spam.sh: name it something obvious

By the way, bash scripts are as easily made as windows batch files. You just put in the code and put a .sh at the end. Inside “del_spam.sh” add the following:

mysql -u username -ppassword -e "delete from wp_comments where comment_approved='spam';" brockangelo

Where “brockangelo” is the name of the database. Repeat this line for each WordPress site you host. Then you need to set this up as a cron job and schedule it for once a night. Or, you can download my plugin that deletes spam daily. Delete Spam Daily plugin.

By the way, the username and password format in that command you see above looks wrong, but it is typed correctly. You put “-u (space) username” then you squish the “-p” and “password” together like this “-u username -ppassword”.

How to Add Uploaded Media to WP-DownloadManager

WARNING: All of the recommendations in this post work off of a test database. The following commands could damage your live site if not tested first. Always work off a test site, then perform a database backup before doing any of these things.

I was faced with a challenge at work this week: I wanted to track how many downloads our forms were getting on the company intranet (built on WordPress). I had already uploaded over one thousand forms to the site. I needed an upload tool that would be able to look at all of the forms and other media that we have accumulated. Unfortunately, none of the download counter plugins for WordPress will look add existing media, and they all want you to upload your forms (or media) through their “Add download” button.

Well, if I’d uploaded only a couple of forms, this wouldn’t be a problem. But since I was dealing with one thousand, I needed a way to import existing uploads into the new download counter. This would have to be done in MySQL.

I looked at download counters on wordpress.org and found one that I liked: WP-DownloadManager. There seem to be quite a few of them, including WP-DownloadCounter (which I also liked) but I chose this one simply because it had been around longer and had a very high rating.

WP-DownloadManager has a way to import existing forms from the management page, but they have to be done one at a time, and you have to manually type all of your titles over again. This would take way too long. So I came up with a way to migrate all of my uploaded form titles and filenames to the WP-DownloadManager’s database.

Search and Replace a Custom Field in WordPress using PHPMyAdmin

Search and Replace a Custom Field in WordPress using PHPMyAdmin

If you’ve ever used Microsoft Excel, you probably have an idea of just how easy changing hundreds of cells can be. Gone are the days of manually typing line by line any corrections that you need to make. The same is true when you are working with a database like we are in WordPress.

This example performs a search and replace of all custom fields that meet a specified criteria. In this case, I’m looking for all staff member pages that have a custom field for a department phone number that was incorrect. We want to replace that with a new number. Using PHPMyAdmin and running a SQL query, I can select all of them to see the results before I do anything to my databse:

SELECT * FROM `wp_postmeta` WHERE `meta_key` LIKE 'DeptPhone' AND `meta_value` LIKE '212-555-1212'

It is worth mentioning, that you can also use wildcards if you want to include more results. The ‘%’ is your wildcard:

SELECT * FROM `wp_postmeta` WHERE `meta_key` LIKE '%eptPhone' AND `meta_value` LIKE '212-555-%'

This yields the same results. It is also worth pointing out that the quotation mark around our text values and the tick mark around our table column names are different.

  • `meta_key` uses tick marks (this is usally the top left key on your keyboard, next to the 1)
  • '%DeptPhone' uses a single quotation mark (this is next to the enter key)

So, to perform the changes, we need to use the UPDATE command instead of select, and actually replace text. Here goes:

UPDATE `wp_postmeta` SET `meta_value` = replace(meta_value, '212-555-1212', '212-444-1212') WHERE `meta_key` LIKE 'DeptPhone'

Search and Replace MySQL using PHPMyAdmin

Search and Replace MySQL using PHPMyAdmin

This is a life-saver: an easy way to search any table inside your database and replace one string of text with another. It works like a charm. But be careful, search first to see the results of your query before you commit the changes, and always backup prior to any database work.

Here is how you test the waters safely:

SELECT * FROM `wp_posts` WHERE `post_content` LIKE '%oops%'


SELECT * FROM `table_name` WHERE `field_name` LIKE '%unwanted_text%'

And here is how you do some damage:

UPDATE `wp_posts` SET `post_content` = replace(post_content, 'oops', 'much better')

And translated:

UPDATE `table_name` SET `field_name` = replace(same_field_name, 'unwanted_text', 'wanted_text')

Removing Categories from your RSS feed

Removing Categories from your RSS feed:
It’s pretty straightforward. Just go into your feed files and add a filter saying: “if the post is in one of these categories, don’t include it in the feed”. Well, that’s the basic concept. Here are the details:I edited the /wp-includes/feed-rss.php & /wp-includes/feed-rss2.php You probably should also do the atomfeeds and others found inside wp-includes, but everyone who is subscribed to our blog uses Google Reader, and afaik, Google uses RSS, so I didn’t bother. I added the following lines just after the beginning of the loop and right after the

while have_posts : the_post:

Add this php code:

if ((!(in_category(53))) && (!(in_category(55))) && (!(in_category(56)))) : {

Where 53, 55 & 56 are categories that you don’t want in your feed. Notice that this is an if statement and this ends with an opening curly brace. So all you have to do is close this if statement.

You must close the if statement at the bottom of the file by issuing a closing curly brace and an endif statement just before the endwhile. So the bottom of my “feed-rss.php” looks like this:

<?php while (have_posts()) : the_post(); ?>
<?php if ((!(in_category(53))) && (!(in_category(55))) && (!(in_category(56)))) : { ?>
<title><?php the_title_rss() ?></title>
<?php if (get_option('rss_use_excerpt')) { ?>
<description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
<?php } else { // use content ?>
<description><?php the_content_rss(”, 0, ”, get_option(’rss_excerpt_length’)) ?></description>
<?php } ?>
<link><?php permalink_single_rss() ?></link>
<?php do_action(’rss_item’); ?>
<?php } endif; ?>
<?php endwhile; ?>

The Codex article can be found here: Customizing Feeds.