0

To add a 'spam/delete' button to an individual comment, you can use code similar to this

$link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '" title="' . __( 'Edit comment' ) . '">' . $link . '</a>';

$link = $link . ' <a href="'.admin_url("comment.php?action=cdc&dt=deletecomment&c=$id").'">Delete/Trash</a> ';

$link = $link . ' <a href="'.admin_url("comment.php?action=cdc&dt=spam&c=$id").'">Spam</a>';

$link = $before . $link . $after;

and add that code via the edit_comment_link filter. There is also this answer which adds the same capability via a plugin: https://wordpress.stackexchange.com/a/54122/29416 .

Either way, you are shown the admin page for that comment, where you can view the comment and then decide to spam/trash the comment. I have adjusted the code to only allow the admin to see those links.

How would this be enhanced to just 'do it', bypassing the admin page? In other words: hit the spam link (as admin), and the comment status is immediately changed to 'spam'.

1
  • I posted an answer, but one thing I didn't include, is that are you sure you want to use the edit_comment_link hook? Because IMO it's supposed to be just a link to edit the comment, so you should (or I would) try looking for a different hook to add the other action links like "Spam".
    – Sally CJ
    Jun 24 at 14:05

1 Answer 1

0

In the comments list table at wp-admin/edit-comments.php, the URL for the "Spam" link as of writing looks like this:

https://example.com/wp-admin/comment.php?c=123&action=spamcomment&_wpnonce=xxxxxxxxxx

where 123 is the comment ID and the _wpnonce value is a nonce with delete-comment_<comment ID> being the action.

And the "Trash" and "Delete" links also follow the same format, except that the action value is trashcomment and deletecomment respectively.

So you should use the same format and visiting the link would just 'do it', i.e. bypassing the "Moderate Comment" admin page. And by default, after successfully marking the comment as spam or after trashing/deleting the comment, you would be sent back to where you were or otherwise then it defaults to wp-admin/edit-comments.php.

And with your code, you'd just need to change this part:

$link = $link . ' <a href="'.admin_url("comment.php?action=cdc&dt=deletecomment&c=$id").'">Delete/Trash</a> ';

$link = $link . ' <a href="'.admin_url("comment.php?action=cdc&dt=spam&c=$id").'">Spam</a>';

to the following, but note that you should always escape a URL when displaying it to the user:

$del_nonce = wp_create_nonce( "delete-comment_$id" );

$link = $link . ' <a href="'.admin_url("comment.php?c=$id&action=deletecomment&_wpnonce=$del_nonce").'">Delete/Trash</a> ';

$link = $link . ' <a href="'.admin_url("comment.php?c=$id&action=spamcomment&_wpnonce=$del_nonce").'">Spam</a>';

See WP_Comments_List_Table::handle_row_actions() for the current URL format for the above links.

And just for reference, this is the full code I used:

add_filter( 'edit_comment_link', function ( $link, $comment_id ) {
    if ( ! current_user_can( 'edit_comment', $comment_id ) ) {
        return $link;
    }

    $del_nonce = wp_create_nonce( "delete-comment_$comment_id" );

    $url = admin_url( "comment.php?c=$comment_id&action=spamcomment&_wpnonce=$del_nonce" );
    $link .= ' | <a href="' . esc_url( $url ) . '">Spam</a>';

    $url = admin_url( "comment.php?c=$comment_id&action=trashcomment&_wpnonce=$del_nonce" );
    $link .= ' | <a href="' . esc_url( $url ) . '">Trash</a>';

    $url = admin_url( "comment.php?c=$comment_id&action=deletecomment&_wpnonce=$del_nonce" );
    $link .= ' | <a href="' . esc_url( $url ) . '">Delete</a>';

    return $link;
}, 10, 2 );
1
  • Thanks for the sample code, which works quite well. I could see the link in the admin screen to do the choice, but was hung up on the nonce value. My assumption was that I had to get a 'comment nonce' value somehow, rather than creating the nonce to use in the link, as you have done. My next issue is how to redisplay the comment list with a positioning of the browser viewpoint at the spot where the comment used to be. I cant's use the comment ID to reposition, as that comment is no longer visible. Thanks. 2 days ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.