Add delete, approve, spam Links to Comments

The question:

Anyone know how to add quick comment moderation links for users who have permission to add/edit posts and comments? (Upprove / Unappove / Edit / Spam / Trash ). Note: in my comments.php I call the loop with <?php wp_list_comments(); ?>

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Per default wp_list_comments() calls the class Walker_Comment. Its method start_el() calls edit_comment_link() and here we find a filter for your question: It is called 'edit_comment_link' and it passes two variables, the link text and the comment ID, which we can use.

The URLs to mark a comment as spam or to delete it are:

  • wp-admin/comment.php?c=1&action=cdc&dt=spam for spam, and
  • wp-admin/comment.php?c=1&action=cdc for deletion.

We can add a parameter redirect_to= to send us back to the post after the comment was trashed.

Here is a sample plugin I just hacked together (GitHub address):

<?php # -*- coding: utf-8 -*-
/**
 * Plugin Name: T5 Comment moderation links
 * Version:     2012.06.04
 * Author:      Thomas Scholz <[email protected]>
 * Author URI:  http://toscho.de
 * License:     MIT
 * License URI: http://www.opensource.org/licenses/mit-license.php
 */

if ( ! function_exists( 't5_comment_mod_links' ) )
{
    add_filter( 'edit_comment_link', 't5_comment_mod_links', 10, 2 );

    /**
     * Adds Spam and Delete links to the Sdit link.
     *
     * @wp-hook edit_comment_link
     * @param   string  $link Edit link markup
     * @param   int $id Comment ID
     * @return  string
     */
    function t5_comment_mod_links( $link, $id )
    {
        $template = ' <a class="comment-edit-link" href="%1$s%2$s" rel="nofollow noreferrer noopener">%3$s</a>';
        $admin_url = admin_url( "comment.php?c=$id&action=" );

        // Mark as Spam.
        $link .= sprintf( $template, $admin_url, 'cdc&dt=spam', __( 'Spam' ) );
        // Delete.
        $link .= sprintf( $template, $admin_url, 'cdc', __( 'Delete' ) );

        // Approve or unapprove.
        $comment = get_comment( $id );

        if ( '0' === $comment->comment_approved )
        {
            $link .= sprintf( $template, $admin_url, 'approvecomment', __( 'Approve' ) );
        }
        else
        {
            $link .= sprintf( $template, $admin_url, 'unapprovecomment', __( 'Unapprove' ) );
        }

        return $link;
    }
}

Screenshot with TwentyEleven (order reversed by the stylesheet):

enter image description here

Method 2

This is what I use (added as a reference — toscho’s answer IS BETTER for many reasons):

<?php if (current_user_can('edit_post')) {
    $id = get_comment_ID();
    echo ' <a href="'.admin_url(" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"comment.php?action=cdc&c=$id").'">[Delete]</a>';
    echo ' <a href="'.admin_url(" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"comment.php?action=cdc&dt=spam&c=$id").'">[Spam]</a>';
} ?>

Most themes already come with an “Edit” link, so I didn’t include that.

Example Preview:

Delete Spam


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment