?? I’ve also needed to do a few Doh! posts myself from not reading carefully enough the first time around. Sorry for the late reply, I was out of communication for a while.
Yes, you’ve got the right idea, both on comment visibility and deleting (deleting as the default condition anyway, the functionality can be altered). Ideally a comment author front end delete functionality is best done using an AJAX approach which is a bit involved if you have never done that sort of thing in WordPress. As it happens, I just helped another forum member with the same need, also with limited PHP and javascript skills.
I came up with some simple, non-AJAX code that helped him, I’ll share it here as I couldn’t subject anyone to the original thread. (The one on this sub-form with many dozens of posts).
The main delete function goes on your child theme’s functions.php.:
// Front end comment delete
add_action('init', 'bcw_front_delete');
function bcw_front_delete() {
if (array_key_exists('bcw_task', $_GET)) {
if ('del' == $_GET['bcw_task']) {
check_admin_referer('del-comment'); //only accept valid requests
global $wpdb;
if ( current_user_can('moderate_comments') || $_GET['user_id'] == get_current_user_id()) {
wp_set_comment_status( $_GET['com_id'], 'trash');
// delete all associated meta
$wpdb->delete( $wpdb->commentmeta, array('comment_id' => $_GET['com_id']));
}
}
}
return;
}
You can delete the meta data line for default comments, it works well if you’ve added other comment fields that are stored in comment meta. You will also need the following hook in functions.php to get the comment counts coordinated with what’s seen. (If your theme displays comment counts in the same way as the default themes that is):
//adjust comment count for single page visibility
add_filter('get_comments_number', 'bcw_adjust_comment_count');
function bcw_adjust_comment_count( $count ) {
global $wp_query;
if ( !is_single()) return $count;
return $wp_query->comment_count;
}
To add a delete link to the comment display that sends the proper data to the first above function, add the following code to the function that displays each comment. This is usually a callback to wp_list_comments()
. Check your theme’s comments.php file to see how comment display is handled.
$user_id = get_current_user_id();
$nonce = wp_create_nonce('del-comment');
$link = get_permalink();
if ( current_user_can('moderate_comments') || $comment->user_id == $user_id ) {
echo " | <a href=\"$link?bcw_task=del&com_id={$comment->comment_ID}&user_id=$user_id&_wpnonce=$nonce\" title=\"Move immediately to trash (no confirmation)\">Delete</a>";
}
It sounds like you figured out the visibility thing, but FYI, here is how I changed the query results to limit visibility (also goes on functions.php):
//Alter comment results if not admin or editor to only have current user's comments
add_filter('comments_array', 'bcw_current_user_comments');
function bcw_current_user_comments( $comments ) {
if ( ! current_user_can('moderate_comments')) {
$comments = array_merge( array_filter( $comments, function( $comment ) {
if ( $comment->user_id == get_current_user_id() ) return true;
return;
}));
}
return $comments;
}