Restrict user from creating topic on certain bbPress Forums
Yesterday, one of our members( Joshua ) asked me how to restrict users from creating topic in specific bbPress based forum but allow users to reply them to the topic. I looked at the filters and put some code that does it. I am sharing it here hoping that some of you might find it useful.
You can put the following code in your theme's functions.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | function buddydev_bbp_restrict_topic_creation( $can ) { $forum_id = 222;//change your forum id if ( ! bbp_is_user_keymaster() && bbp_is_single_forum() && $forum_id == bbp_get_forum_id() ) { $can = false; } return $can; } add_filter( 'bbp_current_user_can_publish_topics', 'buddydev_bbp_restrict_topic_creation' ); function buddydev_restrict_from_posting_topic( $forum_id ) { $restricted_forum_id = 222; if ( ! bbp_is_user_keymaster() && $forum_id == $restricted_forum_id ) { //set error on bbpress and it will not allow creating topic //not the best idea but I personaly don't like the way bbpress does not provide any forum info at other places to hook bbp_add_error( 403, __( 'Not allowed' ) ); } } add_action( 'bbp_new_topic_pre_extras', 'buddydev_restrict_from_posting_topic' );// |
In the above code, I am using forum id 222 as an example, you can change that to your own forum id or even extend the code to include multiple forums ( An example is given below now)
Restricting to multiple forums:-
Added this to make it easy for restricting multiple forums.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | /** * Check if access to this forum is restricted * * @param int $forum_id * * @return bool */ function buddydev_is_restricted_forum( $forum_id ) { $restricted_forum_ids = array( 222, 271, 333 ); //change it with your forum ids if ( in_array( $forum_id, $restricted_forum_ids ) ) { return true; } return false; } /** * Restrict capability to create new topic * * @param $can * * @return bool */ function buddydev_bbp_restrict_topic_creation( $can ) { //if not key master and current forum is restricted if ( ! bbp_is_user_keymaster() && bbp_is_single_forum() && buddydev_is_restricted_forum( bbp_get_forum_id() ) ) { $can = false; } return $can; } add_filter( 'bbp_current_user_can_publish_topics', 'buddydev_bbp_restrict_topic_creation' ); /** * Restrict post request for new topic * @param $forum_id */ function buddydev_restrict_from_posting_topic( $forum_id ) { if ( ! bbp_is_user_keymaster() && buddydev_is_restricted_forum( $forum_id ) ) { //set error on bbpress and it will not allow creating topic //not the best idea but I personaly don't like the way bbpress does not provide any forum info at other places to hook bbp_add_error( 403, __( 'Not allowed' ) ); } } add_action( 'bbp_new_topic_pre_extras', 'buddydev_restrict_from_posting_topic' );// |
Bonus: Restrict replies to bbpress forum topic by the topic id.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /** * Restrict replies for certain topics * * @param bool $allow whether to allow. * * @return bool */ function buddydev_restrict_topic_reply( $allow ) { if ( ! bbp_is_single_topic() ) { return $allow; } $restricted_topic_ids = array( 32, 47 ); //update with your own topic ids. if ( in_array( bbp_get_topic_id(), $restricted_topic_ids ) && ! is_super_admin() ) { $allow = false; } return $allow; } add_filter( 'bbp_current_user_can_access_create_reply_form', 'buddydev_restrict_topic_reply' ); |
Thanks again for your help! This worked perfectly!
Thank you 🙂
Hi, I saw your post titled; "Restrict user from creating topic on certain bbPress Forums"
The code you provided seem to be just what I've been looking for and am currently thrilled to have come across it. One thing that I've been having trouble achieving is the part where you mention :
"…you can change that to your own forum id or even extend the code to include multiple forums."
I've been able to successfully change the forum id but was not able to "extend" the code to add more forum ids. Could you give some more specific directions as to HOW I need to "extend" the code?
Thank you so much in advance
Hi Tyler,
Thank you for the comment.
I have added the section to show how to restrict multiple forums at the bottom of the post. Hope that helps.
Wow that was quick! Thanks so much ! it works perfectly!
Very much appreciated!
You are most welcome 🙂
Hi,
Do you know a way how to disable reply on certains topics ? Only admin can post reply.
Thanks in advance.
Regards
Thank you for asking. I have added an example at the bottom of the post.
Hope that helps.