• Skip to main content
  • Skip to primary sidebar

Technical Notes Of
Ehi Kioya

Technical Notes Of Ehi Kioya

  • About
  • Contact
MENUMENU
  • Blog Home
  • AWS, Azure, Cloud
  • Backend (Server-Side)
  • Frontend (Client-Side)
  • SharePoint
  • Tools & Resources
    • CM/IN Ruler
    • URL Decoder
    • Text Hasher
    • Word Count
    • IP Lookup
  • Linux & Servers
  • Zero Code Tech
  • WordPress
  • Musings
  • More
    Categories
    • Cloud
    • Server-Side
    • Front-End
    • SharePoint
    • Tools
    • Linux
    • Zero Code
    • WordPress
    • Musings
Home » WordPress » Modify bbPress Roles To Allow Participants Trash Their Own Topics And Replies

Modify bbPress Roles To Allow Participants Trash Their Own Topics And Replies

By Ehi Kioya Leave a Comment

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.

Found this article valuable? Want to show your appreciation? Here are some options:

  1. Spread the word! Use these buttons to share this link on your favorite social media sites.
  2. Help me share this on . . .

    • Facebook
    • Twitter
    • LinkedIn
    • Reddit
    • Tumblr
    • Pinterest
    • Pocket
    • Telegram
    • WhatsApp
    • Skype
  3. Sign up to join my audience and receive email notifications when I publish new content.
  4. Contribute by adding a comment using the comments section below.
  5. Follow me on Twitter, LinkedIn, and Facebook.

Related

Filed Under: Backend (Server-Side), PHP, Programming, WordPress Tagged With: bbPress, Forums, WordPress

About Ehi Kioya

I am a Toronto-based Software Engineer. I run this website as part hobby and part business.

To share your thoughts or get help with any of my posts, please drop a comment at the appropriate link.

You can contact me using the form on this page. I'm also on Twitter, LinkedIn, and Facebook.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

23,738
Followers
Follow
30,000
Connections
Connect
14,568
Page Fans
Like
  • Recently   Popular   Posts   &   Pages
  • Actual Size Online Ruler Actual Size Online Ruler
    I created this page to measure your screen resolution and produce an online ruler of actual size. It's powered with JavaScript and HTML5.
  • WordPress Password Hash Generator WordPress Password Hash Generator
    With this WordPress Password Hash Generator, you can convert a password to its hash, and then set a new password directly in the database.
  • How To Change A SharePoint List Or Library URL How To Change A SharePoint List Or Library URL
    All versions of the SharePoint user interface provide an option to change the title (or display name) of a list or library. Changing SharePoint library URL (or internal name), however, is not exactly very intuitive. We will discuss the process in this article.
  • About
  • Contact

© 2022   ·   Ehi Kioya   ·   All Rights Reserved
Privacy Policy