3

I'm trying to output a "delete comment" link on the front-end. Here's my attempt so far:

printf(
    '<a href="%s">%s</a>',
    wp_nonce_url(
        admin_url( "comment.php?c=$comment_id&action=deletecomment" ),
        'delete-comment_' . $comment_id
    ),
    __( 'Delete comment', 'text-domain' )
);

I've added edit_comment cap to the author role. However, when I log in as an author and click on Delete comment, I get the following message on a white screen of death:

Sorry, you are not allowed to edit comments on this post.

Thank you for creating with WordPress.

Version 4.7.4

How can I output a permanently delete comment link which works?

3

After briefly testing, the code snippet from OP seems to work:

printf(
    '<a href="%s">%s</a>',
    wp_nonce_url(
        admin_url( "comment.php?c=$comment_id&action=deletecomment" ),
        'delete-comment_' . $comment_id
    ),
    esc_html__( 'Delete comment', 'text-domain' )
);

But it looks like we have to make sure that the author is only deleting comments on hir own post, otherwise it will look for the edit_others_posts and edit_published_posts primitive capabilities.

The edit_comment is not a primitive capability, so instead we have to look at the map_meta_cap() function to see what primitive capabilities it relies on. There we can see that edit_comment is a meta capability that uses:

$caps = map_meta_cap( 'edit_post', $user_id, $post->ID );

for non-orphaned comments. Then we have to look at the part where the edit_post meta capability maps to other primitive capabilities. There are few possible mappings there, e.g. to edit_others_posts and edit_published_posts primitive capabilities.

Jean Galea has written a great article on roles and capabilities, where it says:

  • Primitive capabilities are assigned to user roles.
  • Meta capabilities never should be assigned to a role.
  • Strange! I did try deleting own comments only. Can I ask how you're adding the edit_comment cap? Maybe that's what I'm doing wrong. My approach is $role = get_role( 'author' ); $role->add_cap( 'edit_comment' ); and I'm hooking to after_switch_theme – henrywright Jun 12 '17 at 21:01
  • I've figured out what's going on here. Using my snippet, you can delete your own comments if, and only if, that comment is associated with your own post. If you comment on another author's post, you can't delete the comment even with the edit_comment cap. – henrywright Jun 12 '17 at 21:43
  • I updated the answer @henrywright – birgire Jun 12 '17 at 22:50
  • Thanks @birgire for making sense of this. edit_post isn't documented here but I guess it is a capability belonging to the post author only. Hence why the comment author can't delete their comment(s) in such cases. – henrywright Jun 12 '17 at 22:57
  • 1
    Making a note here that the map_meta_cap hook was the solution for me. It'll allow me to use custom mappings instead of the default mappings used by WordPress in map_meta_cap(). – henrywright Jun 13 '17 at 22:11

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.