Backwards-Compatible Spam and Delete Buttons for WordPress
Post #640 categorized as WordPress, last updated on acid
Tagged with code, comments, links, php, tips, tricks, 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! Joost’s code 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.
Share this..
Related articles
- WordPress Tip: Remove Spam from the Comment Subscription Manager
- WordPress Spam Battle: 3 Seconds that will Save You Hours of Time
- Block Spam by Denying Access to No-Referrer Requests
- WordPress Tip: Valid, SEO-Friendly Email Permalink Buttons
- WordPress Tip: Quick Hack to Block Spam for the Wordspew Shoutbox Chat Plugin
- WordPress Search Function Notes
- WordPress RDF Source Makeover
#1 — Benjamin Sterling
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.