Having a forum you need to keep an eye on it because it can quickly transform into a habitat of spammers and trash posters. There are a lot of ways to make sure that your forum participants get the best possible experience without ads and scams. One of them is to carefully moderate new topics your users create.
Unfortunately, bbPress lacks some seemingly basic moderation features. For example, there’s no default setting allowing you to make all new topics pending by default. However, this feature can be easily added to your bbPress project with some custom code.
Introduction
By default, if the current user has the capability to create topics, their newly created topic will be published automatically after the user presses the submit button.
However, it’s good to have a moderation policy by which new topics should be checked by a moderator first.
Under the hood, all bbPress topics are a custom WordPress post type called topic. So just as the regular WordPress posts, they have the post_status property by which WordPress understands how to handle them on the frontend.
So, if we want to manually approve or disapprove every new topic, we need to change the default bbPress topic status. Let’s see how to achieve this goal.
How to change the default bbPress topic status?
To modify a topic before it is inserted into the database, we can use a special filter hook called bbp_new_topic_pre_insert. This filter hook allows us to modify post parameters like post_author, post_title, post_content, etc. So it can be useful in other cases when you need to somehow modify a topic before it is inserted into the database.
$topic_data = apply_filters( 'bbp_new_topic_pre_insert', array(
'post_author' => $topic_author,
'post_title' => $topic_title,
'post_content' => $topic_content,
'post_status' => $topic_status,
'post_parent' => $forum_id,
'post_type' => bbp_get_topic_post_type(),
'tax_input' => $terms,
'comment_status' => 'closed'
) );
But today the only parameter we need to modify is the fourth one — post_status. So, to make all new topics pending by default, we need to use a filter function like this:
add_filter( 'bbp_new_topic_pre_insert', 'bbp_change_default_topic_status' );
function bbp_change_default_topic_status( $args ) {
$args['post_status'] = 'pending';
return $args;
}
Pretty simple, as you see. Let’s make it not so boring and add some more logic there.
How to make all new user’s topics pending if a user is not a moderator?
Obviously, it makes no sense to manually approve new topics created by your admins and moderators. If they have such capabilities, you already trust them.
In the official bbPress documentation, you can find a comprehensive list of the default bbPress roles and their default capabilities. Both keymasters and moderators have the moderate capability, so we can use to check if a user is a regular user which topics should be moderated.
Also, keep in mind that participants that were marked as Forum Moderators in a forum’s settings won’t have the moderate capability. To publish their new topics without moderation, you should check if the current user’s ID is amongst the current forum moderators.
At the end of the day, your code snippet should look like this:
add_filter( 'bbp_new_topic_pre_insert', 'bbp_change_default_topic_status' );
function bbp_change_default_topic_status( $args ) {
$topic_needs_moderation = true;
$current_user_id = get_current_user_id();
$current_forum_id = bbp_get_forum_id();
$forum_moderators = bbp_get_moderator_ids( $current_forum_id );
// If a user is a keymaster or a global moderator
if ( current_user_can( 'moderate' ) ) {
$topic_needs_moderation = false;
}
// If a user is a moderator of the current forum only
if ( in_array( $current_user_id, $forum_moderators ) ) {
$topic_needs_moderation = false;
}
if ( $topic_needs_moderation ) {
$args['post_status'] = 'pending';
}
return $args;
}
Summary
There are a lot of different ways to modify both code snippets above. For example, you can skip the approval process if a user already has at least one published topic. In this case, my previous post about how to limit topics a user can create per day might be helpful for you as well.
Hope you have learned something new from this post.
If you have any questions or issues, feel free to write about them in the comments section under this post or even contact me directly.
Don’t forget to follow me on LinkedIn if you don’t want to miss new posts on my blog.
Thank you for your attention; see you in the next one!
thank you Artemy, I was looking code for bbpress replies’ and topics’ not registered users to moderate. Ian Stanley’s moderation plugin is very useful but lastly 9 years ago updated and therefore dangerous for security. Your solution looking similar. Do you have any working for replies also?
good luck
Hi! To modify new replies before inserting them into the database, you can use a very similar hook called bbp_new_reply_pre_insert.