Skip to content

Adding Spam and Delete Button in WordPress Comment Section Using PHP

By default, WordPress allows administrators to manage comments from the dashboard. However, you can make moderation easier by adding Spam and Delete buttons directly in the comment section of your posts. On ruachost.com, this can be achieved with a simple PHP function.

  • Streamlines comment moderation.
  • Saves time by allowing quick action without navigating to the dashboard.
  • Improves site security by reducing spam visibility.
  1. Log in to WordPress with an administrator account.

  2. In the dashboard, go to Appearance → Theme Editor.

  3. Select your active theme and open the functions.php file.

  4. Add the following code snippet:

    // Add spam and delete links to comments
    function ruachost_spam_delete_comment_link($id) {
    global $comment, $post;
    // Check user permissions
    if ($post->post_type == 'page') {
    if (!current_user_can('edit_page', $post->ID)) return;
    } else {
    if (!current_user_can('edit_post', $post->ID)) return;
    }
    $id = $comment->comment_ID;
    $link = '<a href="' . admin_url("comment.php?action=cdc&c=$id") . '">Delete</a>';
    $link .= ' | <a href="' . admin_url("comment.php?action=cdc&dt=spam&c=$id") . '">Spam</a>';
    return $link;
    }
    add_filter('edit_comment_link', 'ruachost_spam_delete_comment_link');
  5. Save the file.

  6. Refresh your site → Each comment will now display Delete and Spam links for administrators.

  • Always back up your theme before editing functions.php.
  • Use a child theme to preserve changes during updates.
  • Only administrators or users with proper permissions will see these links.