USharing
开放博客

Allowing subscriber role to delete their own posts

The capability required to delete posts is delete_posts. If you want them to be able to delete their own published posts, the capability is delete_published_posts.

The capability required to view the administration panel is read. Subscribers have this capability natively, so unless you have removed it, subscribers can access the backend.

I would write a simple plugin that upon activation adds the required capabilities to the subscriber role and upon deactivation removes those caps.

Then in your theme, you can check for:

if( current_user_can( 'delete_posts' ) ) {
  //* Show delete link
}

 

Because the subscriber role doesn’t have the capability to delete_others_posts, the link will not show on posts that they didn’t author, and they will not be able to delete posts that they did not publish.

/**
 * Plugin Name: WordPress StackExchange Question 268755
 * Description: Allow subscribers to delete their own posts
 **/

//* On activation, add the capabilities to the subscriber role
register_activation_hook( __FILE__, 'wpse_268755_activation' );
function wpse_268755_activation() {
  $subscriber = get_role( 'subscriber' );
  $subscriber->add_cap( 'delete_posts' );
  $subscriber->add_cap( 'delete_published_posts' );
}

//* On deactivation, remove the capabilities from the subscriber role
register_deactivation_hook( __FILE__, 'wpse_268755_deactivation' );
function wpse_268755_deactivation() {
  $subscriber = get_role( 'subscriber' );
  $subscriber->remove_cap( 'delete_posts' );
  $subscriber->remove_cap( 'delete_published_posts' );
}

Without giving the user and/or role the capability to delete a post, then they won’t be able to do so, even if you show them a delete link. Likewise, a user or role can delete a post if they have the capability even if you don’t show a delete link, it will just be more difficult for them.

赞(0) 打赏
未经允许不得转载:USharing » Allowing subscriber role to delete their own posts

觉得文章有用就打赏一下文章作者

微信扫一扫打赏