Code snippet for modifying bbPress forum participants capabilities. With this snippet active, your forum participants will be able to trash their own topics and replies.
This will not allow for complete delete. An administrator will still be able to view the topic (or reply) in WordPress trash (wp-admin backend) and restore it if necessary. Moderators cannot view or action trashed content.
add_filter('bbp_get_caps_for_role', 'ehi_add_role_caps_filter', 10, 2); function ehi_add_role_caps_filter($caps, $role) { if($role == bbp_get_participant_role()) { $newcaps = array( 'spectate' => true, 'participate' => true, 'read_private_forums' => true, 'publish_topics' => true, 'edit_topics' => true, 'delete_topics' => true, 'view_trash' => true, 'publish_replies' => true, 'edit_replies' => true, 'delete_replies' => true, 'assign_topic_tags' => true, ); return $newcaps; } return $caps; } add_filter('bbp_map_reply_meta_caps', 'ehi_tweak_trash_meta_caps', 11, 4); add_filter('bbp_map_topic_meta_caps', 'ehi_tweak_trash_meta_caps', 11, 4); function ehi_tweak_trash_meta_caps($caps, $cap, $user_id, $args) { // Apply filter for only the "delete_reply" and "delete_topic" capabilities if ($cap == "delete_reply" || $cap == "delete_topic") { $_post = get_post($args[0]); if (!empty( $_post )) { $post_type = $_post->post_type ; // If the user is inactive (spam or deleted), add the "do_not_allow" capabality if (bbp_is_user_inactive($user_id)) { $caps[] = 'do_not_allow'; } elseif (user_can( $user_id, 'moderate')) { // Moderators have power $caps[] = 'moderate'; } elseif (user_can($user_id, 'participate')) { if (((int)$user_id === (int)$_post->post_author)) { // The user has the "participate" capability. So allow them to trash content. if ($post_type == bbp_get_reply_post_type()) { // They can delete any of their replies $caps = array('delete_replies'); } if ($post_type == bbp_get_topic_post_type()) { // They can only delete their topic if it has no replies $reply_count = bbp_get_topic_reply_count(); if ($reply_count == 0) { $caps = array('delete_topics'); } } } } else { // We don't know the user's capability. So don't allow them to delete anything. $caps[] = 'do_not_allow'; } } } // Return the modified capabilities return $caps; }
This code is adapted from the work by @robin-w here. I made a couple of edits and added my own comments.
Note 1: For some reason, @robin-w‘s original code (and by extension, my above code) only works when added as a snippet to the Code Snippets plugin. It causes a website error when added to functions.php file. At the time, I did not bother to investigate why the functions.php approach broke the site. I just used the Code Snippets plugin and moved on to other stuff.
Note 2: If you would rather not experiment with the Code Snippets plugin, and if like me you don’t want to waste time troubleshooting the functions.php error caused by the code, the good news is that the original author has now also added this functionality to his popular bbp style pack plugin. Within the plugin’s many options, you will find this feature under Topic/Reply Display >> Allow Participants to trash their own topics.
Leave a Reply