Add delete, approve, spam Links to Comments

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(); ?>

Related posts

Leave a Reply

3 comments

  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 <info@toscho.de>
     * 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">%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

  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("comment.php?action=cdc&c=$id").'">[Delete]</a>';
        echo ' <a href="'.admin_url("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

  3. In 2022, unfortunately, the accepted answer does no longer work… without some changes, at least. You now need to add a nonce or the action will be rejected as per protocol. But this can be amended:

    $template = '<a class="comment-spam-link" href="%1$s">%2$s</a>';
    $admin_url = admin_url( 'comment.php?c=' . $comment_id . '&action=' );
    $nonce_url = esc_url( wp_nonce_url( $admin_url . 'spamcomment', 'delete-comment_' . $comment_id ) );
    
    // $nonce_url = esc_url( wp_nonce_url( $admin_url . 'trashcomment', 'delete-comment_' . $comment_id ) );
    // $nonce_url = esc_url( wp_nonce_url( $admin_url . 'approvecomment', 'approve-comment_' . $comment_id ) );
    // $nonce_url = esc_url( wp_nonce_url( $admin_url . 'unapprovecomment', 'approve-comment_' . $comment_id ) );
    // $nonce_url = esc_url( wp_nonce_url( $admin_url . 'deletecomment', 'delete-comment_' . $comment_id ) );
    

    And used like this, for example, although I strongly recommend adding &redirect_to= as well:

    $output = sprintf( $template, $nonce_url, __( 'Spam' ) );
    

    Taken from the core wp-admin/comment.php.