0

I'd like to allow certain (logged in) users to be able to edit their comments. I don't care if they can only edit through the back-end - obviously front-end editing would be lovely, but I'm willing to take what I can get at this point. Numerous online questions pointed to edit_posts as the cap to add, but that didn't seem to do it. After fooling around with custom roles and capabilities, I noticed that what enabled comment editing was actually edit_others_posts.

I'm baffled. Every answer I've seen mentioning edit_posts has always mentioned the fact that, internally, comments are posts. But with just edit_posts, you can still edit your own posts, while posts authored by others are limited. So if comments are also posts, why do you need to be able to edit anyone/everyone's posts and comments via edit_others_posts in order to edit any comments at all?

Additionally: is there a good solution for this? I need these users to be able to edit indefinitely if possible. I'm assuming this would involve some hooks/filters, because the capabilities alone don't seem to cut it.

0

So my solution may be a little hackish, but it does work. Comments can be edited indefinitely, and only by admins and the registered user that posted the comment.

Part of the issue I'm dealing with is that edit_posts only allows the author of a post to edit all its comments, not necessarily the commenters themselves. In order to allow this, you need to give your editable_commenter role the edit_others_posts capability, so that they can edit comments regardless of whether they authored the parent post. Then, you need to systematically disappear every admin option that would allow them to touch anyone else's posts or comments.

First, remove the comment_row_actions for your editable_commenters on the Comments backend page for every comment that user didn't author. I'm using emails to match users to comments, since it's the least likely piece to change.

// Hide the comments row for non-admins who don't match the email
function edit_own_comments_backend($actions, $comment) {
    if(is_user_logged_in()) {
        $comment_email = get_comment_author_email($comment->comment_ID);
        $email = wp_get_current_user()->user_email;

        if ($comment_email != $email && !current_user_can('administrator')) {
            unset($actions['inline hide-if-no-js']);
            unset($actions['edit']);
            unset($actions['trash']);
            unset($actions['approve']);
            unset($actions['unapprove']);
            unset($actions['spam']);
            unset($actions['delete']);
            unset($actions['quickedit']);
            unset($actions['reply']);
        }
    }

    return $actions;
}

add_filter('comment_row_actions', 'edit_own_comments_backend', 10, 2);

In my case, the editable_commenters role is solely a commenter - they should never be able to edit, create, or delete any posts. So we take away Posts from their sidebar, and also New and Edit from the top-aligned admin bar.

// Hide the "Posts" menu item from all editable_commenters

function hide_others_posts_backend() {
    if (is_user_logged_in() && current_user_can('editable_commenters')) {
        remove_menu_page('edit.php');
    }
}

add_action('admin_menu', 'hide_others_posts_backend');

// Remove "New" and "Edit" from the admin top bar for commenters
function remove_top_admin_nodes($wp_admin_bar) {
    if (current_user_can('editable_commenter')) {
        $wp_admin_bar->remove_node('new-content');
        $wp_admin_bar->remove_node('edit');
    }   
}
add_action('admin_bar_menu', 'remove_top_admin_nodes', 999);

You also need to hide the In Response To column on the Comments backend page, since they'll be able to click on post titles to access the edit window still, but I haven't added that yet.

Lastly, on the post page, if you'd like to keep the Edit links but want to limit them to only the user's posts, include this.

// Show or hide the edit link on comments if you are/aren't the author
function show_single_edit_comments($link, $comment_id, $text) {
    if(is_user_logged_in()) {
        $comment_email = get_comment_author_email($comment_id);
        $email = wp_get_current_user()->user_email;

        if ($comment_email == $email || current_user_can('administrator')) {
            return $link;
        }
        else {
            return false;
        }
    }

    return false;
}

add_filter('edit_comment_link', 'show_single_edit_comments', 10, 3);
|improve this answer|||||

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.