BuddyDev

Search

[Resolved] BBpress – restrict topic creation

  • Participant
    Level: Enlightened
    Posts: 120
    Erich199 on #2281

    Hi,

    I am wondering what is the best way to restrict user roles except key master or moderator from creating a new topic?

    I have a forum for site news and info that I want users to be able to reply to topics but not create them.

    Thanks for your help.

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #2283

    Hi Joshua,
    Thank you for asking.
    I did it for one of my sites. I will look into the code and post it today( a little late today )

    Thank you
    Brajesh

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #2296

    Hi Joshua,
    Do you want to restrict the users from creating in all forums and just reply(It is easy) or on specific forum only?

    My own solution was a bit mixed with template, so not very useful here but there are filters, if it is the first case, It will be super easy to do.

  • Participant
    Level: Enlightened
    Posts: 120
    Erich199 on #2299

    Hi Brajesh,

    Sorry for delay responding. Was out working.

    I’d like to just restrict a single forum that I post site news and info in. Ideally, I’d like to restrict topic creation only, but allow members to reply to created topics.

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #2300

    Hi Joshua,
    Thank you.
    You can put the following code in your theme’s functions.php and it will work

    
    
    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' );//
    
    

    The first filter prevents showing the topic creation form on single forum screen. The second action hook checks at the time of publish and restricts. Most of the time, the first one will suffice but that just hides the form, so I used the other hook to make sure the user is never able to post new topic.

    Please let me know how it goes.

    • This reply was modified 8 years, 10 months ago by Brajesh Singh. Reason: Fixing the bug in code
  • Participant
    Level: Enlightened
    Posts: 120
    Erich199 on #2303

    Hi Brajesh,

    This worked perfectly! Thank you very much. I can now post topics as admin or moderator and the participants can not. This is exactly what I wanted in that forum.

  • Participant
    Level: Enlightened
    Posts: 120
    Erich199 on #2304

    Hi Brajesh,

    I just noticed this is disabling a user from posting in all forums not just the single forum

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #2306

    Hi Joshua,
    I have updated the code.
    There was a logical issue where I used bpp_is_forum() that does not check for current forum just the post type. Have updated the code now.

  • Participant
    Level: Enlightened
    Posts: 120
    Erich199 on #2308

    hi Brajesh,

    Worked like a charm. Thanks so much for your help on this.

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #2311

    You are most welcome. Glad, I was able to help 🙂

The topic ‘ [Resolved] BBpress – restrict topic creation’ is closed to new replies.

This topic is: resolved