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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

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 <<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="80e9eee6efc0f4eff3e3e8efaee4e5">[email protected]</a>>
 * 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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x