Spring Sale! Save 30% on all books w/ code: PLANET24
Web Dev + WordPress + Security

Backwards-Compatible Spam and Delete Buttons for WordPress

Recently, Joost de Valk shared an excellent technique for adding spam and delete buttons to comments on your WordPress-powered blog. The idea is to save administration time by providing links to either “spam” or “delete” individual comments without having to navigate through the WordPress Admin Area. Joost provides the following plug-n-play solution:

// add this code to your theme's function.php file:
<?php function delete_comment_link($id) {
	if (current_user_can('edit_post')) {
		echo '| <a href="'.admin_url("comment.php?action=cdc&c=$id").'">del</a> ';
		echo '| <a href="'.admin_url("comment.php?action=cdc&dt=spam&c=$id").'">spam</a>';
	}
} ?>

// then, add this tag to a location in your comments.php file:
<?php delete_comment_link(get_comment_ID()); ?>

Nice! This code snippet works great for all versions of WordPress that support the admin_url() function, which I think is any version greater than or equal to 2.6. So, to get this code working in older versions (less than v2.6) of WordPress, we need to provide the required path information without using admin_url(). One way of doing this is to replace the version-specific admin_url() function with the get_bloginfo() template tag. We can then use the wpurl parameter to return the WordPress installation directory, which is subsequently echoed in the following reformatted function:

// spam & delete links for all versions of WordPress
<?php function delete_comment_link($id) {
	if (current_user_can('edit_post')) {
		echo '| <a href="'.get_bloginfo('wpurl').'/wp-admin/comment.php?action=cdc&c='.$id.'">del</a> ';
		echo '| <a href="'.get_bloginfo('wpurl').'/wp-admin/comment.php?action=cdc&dt=spam&c='.$id.'">spam</a>';
	}
} ?>

Place this function in your theme’s functions.php file, and then call the function by adding the following code to the desired location in your comments.php file:

<?php delete_comment_link(get_comment_ID()); ?>

And that’s all there is to it! Depending on placement of the function call, your comments area should now feature quick and easy “spam” and “delete” buttons next to each individual comment. Even better, this improved function is version-independent, backwards-compatible, and thus will work for any version of WordPress.

About the Author
Jeff Starr = Designer. Developer. Producer. Writer. Editor. Etc.
Blackhole Pro: Trap bad bots in a virtual black hole.

14 responses to “Backwards-Compatible Spam and Delete Buttons for WordPress”

  1. Benjamin Sterling 2008/12/01 8:48 am

    great tip cause god knows when you are getting a ton of comments its sometimes easier to see the comments in the context of the post.

  2. What a weird idea.
    Why would someone prefer to visit their blog pages, one by one, instead of the admin area where everything is gathered conveniently?

  3. Benjamin Sterling 2008/12/02 6:48 am

    I would say that if you are already on the page and you notice the spam or the comment you want to edit it would be easier they going to your admin area to make the change. But if your concern is having to load and reload things, just apply some Ajax to the mix (with the proper security measures in place of course)

  4. Jeff Starr 2008/12/02 8:40 am

    @Ozh: Blame it on Joost! ;) No, seriously, having access to spam and delete buttons next to each comment makes it super-easy to clean up missed spam, scummy trackbacks, and other garbage. I am continually scouring my old posts’ comments and weeding out junk, which for me is always easier to see from the actual post pages themselves. Thus, having quick and easy access to spam and delete buttons has made my life considerably easier. For me, it’s a real time-saver.

  5. Jeff Starr 2008/12/02 9:31 am

    @Benjamin Sterling: yes, for me, the main benefit here is convenience and expediency. I am always surfing around my site checking pages — especially during updates and redesigns — and frequently clean up junk comments on the spot. Having those buttons right there and ready to go is much faster than checking the comment number, jumping back into admin, searching for the comment, and then deleting it from there. In fact, the very presence of the spam and delete links serves as a constant reminder, encouraging me to stay vigilant in my fight for relevant, spam-free post comments. ;)

  6. Jeff, thanks again for sharing! You never fail to posts stuff that will be of interest to WordPress developers as well as amateurish designers (like me, hah!). I’ve tried out the plug and play method and it worked! I will save me a lot of time – instead of going back and forth between the comment area and the admin panel (which is a frustrating stuff when a new spam bot appears and Akismet or whatever fails to catch it).

    Anyway, users that are using versions of WordPress that are less than 2.6 should be upgrading ;) it’s a little outdated and has too many security holes here and there (think of being a goldfish trapped in a leaky plastic bag!)

    Have a great week ahead! I wonder how’s the holiday festive mood over there. I went downtown yesterday and the lights were pretty nice. Since we don’t really celebrate Christmas over here (we just have an X’mas tree and that’s all. Perhaps a turkey), I don’t have to worry about X’mas shopping. Good luck!

  7. Thanks for sharing knowledge! :)
    By the way, I’d like to let you know I’m having some problems accessing this site behind the ISA server.
    Now I’m in a customer facility without proxy and I can see this site, but when I’m behind the ISA-Proxy server I got a 403 error page :D

    Greetings! :D

  8. Jeff Starr 2008/12/03 4:20 pm

    @H5N1: Great to see you again! :) Sorry to hear about the ISA server issues.. it seems like we have had this conversation before..? Is this the same server/issue we are talking about, or a different one?

  9. Hi Jeff.
    I’m back in my hometown again (Rome, anyway) and I can finally interact with your site :)
    In my customer’s office (yes, the same place frome where we have that conversation) there’s an ISA server functioning as proxy.
    The configuration of this server is very rough, indeed: i.e. it accept SSL connections only at the standard port 443 for HTTPS protocol so we can’t use any site redirecting (you know, for a little more security) the connection to another port.
    The ISA server is running on a Windows 2003 Server SR2 and it’s none of my business since my work there consist in helping people for the clients issues (not the servers issues) :)
    I’d like to say that’s because I have no competence for this, but… well, I’m not payed enough, thou! :)
    I think that your server has strange thoughts (!) about who is visiting what when meet an ISA/proxy HTTP request so it produce a 403 error! ;)

    I hope you can solve it!

  10. Hi H5N1! Great to see you again — sorry for the delayed response; we are currently moving into a new home and it has been crazy trying to unpack and get everything setup, connected and running properly. Now that I am back online, I will try to resolve this issue once and for all.

    Based on the information you have provided, it sounds as if the problem may reside in one (or more) of the directives used in the 3G Blacklist. I am currently not blocking any types of proxy requests, but the 3G Blacklist does contain rules that may be interfering. Plus, the fact that you are getting 403 errors instead of 404s reinforces this hypothesis because the blacklist is configured to return 403s for any blocked requests.

    So, if you can get ahold of a few of the proxy requests used by the ISA server (i.e., the general format of the URLs used to request pages), I will be able to compare the requests to the various character strings blocked by the blacklist. — We can do this!! ;)

  11. Hi Jeff, next week I’ll be in my customer’ offices and then I can provide the entire HTTP request string (thank to Firefox and its addons it’ll be easier!).
    But the problem is that It’ll be difficult to post it if I cannot enter here! :D
    By the way there’s another chance: there they have another DSL line (optic fiber) bet that network is not working (we are trasfering the switches, the servers, cables etc from a place to another one).
    So when the not-filtered-by-proxy line will be available again I can provide all you need.
    The strange thing is that when I wrote my first post this issue came only when I used Firefox but not whit Internet Explorer, but now I cannot access neither using IE or Opera or Google Chrome… strange… uh!?! :D

    By the way I want to wish you happy new year :)

  12. Jeff Starr 2009/01/03 6:22 pm

    Hey H5N1, that sounds good — remember that you can always send code directly to me via email (which is jeff at this domain). Also, the differences in browsers may be related to me testing and tweaking the next (4G) blacklist live on the site. Depending on the different times that you were posting, there may have been different rules in place. But yes, otherwise that is very strange indeed!

    And likewise, a Happy New Year to you as well! :)

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
The Tao of WordPress: Master the art of WordPress.
Thoughts
I live right next door to the absolute loudest car in town. And the owner loves to drive it.
8G Firewall now out of beta testing, ready for use on production sites.
It's all about that ad revenue baby.
Note to self: encrypting 500 GB of data on my iMac takes around 8 hours.
Getting back into things after a bit of a break. Currently 7° F outside. Chillz.
2024 is going to make 2020 look like a vacation. Prepare accordingly.
First snow of the year :)
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.