Book Sale! Code WP2025 takes 20% OFF our Pro Plugins & Books »
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 = Web Developer. Security Specialist. WordPress Buff.
Digging Into WordPress: Take your WordPress skills to the next level.

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

  1. john doedeod 2010/01/15 11:02 pm

    i thought i would also add to this post… when adding this to my theme i found it was not validating until i made the following changes…

    I replaced all (3) instances of the & with the html equivalent &

    After making the changes the site validated fine and functioned as required.

    cheers and thanks Jeff for having such a great site!!!!!!

  2. Awesome, john — thanks for the tip! :)

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 »
.htaccess made easy: Improve site performance and security.
Thoughts
Launching my new plugin, Head Meta Pro 🚀 Complete meta tags for WordPress.
Migrating sites to a new server, so far so good. Please report any bugs, thank you.
Arc browser looked good but lost me at “account required”. No browsers do that.
Finishing up the pro version of Head Meta Data plugin, launch planned this month.
Finally finished my ultimate block list to stop AI bots :) Blocks over 400+ AI bots!
After 10 years working late at night, my schedule has changed. I am now a “morning person”, starting my day at 6am or earlier.
Nice update for Wutsearch search engine launchpad. Now with 19 engines including Luxxle AI-powered search.
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.