At the dawn of the Internet and online communities, forums were a thing. Today, some people say that forums are dead because of social media websites and messengers. However, some niche communities still utilize forums as a place to communicate, share content and ideas. And bbPress is one of the easiest ways to set up a forum on your WordPress website.
This post is a start of a new category in my blog. As you might have guessed, I will tell you how to modify and extend the bbPress plugin. And in today’s post, we’re going to take a look at how to limit the number of topics a user can create per day.
Introduction
By default, every user with at least Participant user role can create as many topics as they want. bbPress follows the same philosophy as BuddyPress: the plugin contains only the basic features, but you can extend it by using additional plugins and add-ons.
You can’t do this by default if you want to limit topics a user can create per day or week. I also didn’t find any add-ons that can do something like this, so I decided it would be an excellent idea for a new post.
Let’s imagine that you have a pretty active forum. Your Participants create topics every day or maybe even every hour. Some people may abuse the topic creation feature even if you have some spam filtering and user approval process.
One of the ways to prevent the creation of spam topics is to limit the number of topics a user can create for a certain period.
How to get the number of topics a user has created for a certain period
Every bbPress topic is an instance of a custom post type – topic. This means that we can create a basic WP_Query instance with some date arguments to get the topics a user has started for a certain period.
In our case, we need to get the topics a user has created today.
function get_user_topics_started_today( $user_id ) {
$today = getdate();
$query = new WP_Query( array(
'post_type' => 'topic',
'author' => $user_id,
'fields' => 'ids',
'year' => $today['year'],
'monthnum' => $today['mon'],
'day' => $today['mday'],
) );
return $query->found_posts;
}
If you want to get the number of posts a user has created for the last 7 days, you can rewrite this code like this:
function get_user_topics_started_this_week( $user_id ) {
$query = new WP_Query( array(
'post_type' => 'topic',
'author' => $user_id,
'fields' => 'ids',
'date_query' => array(
array( 'after' => '1 week ago' )
)
) );
return $query->found_posts;
}
How to limit the number of topics a user can create in bbPress
In the source code of bbPress, you can find an interesting filter hook. It’s called bbp_current_user_can_access_create_topic_form. It’s a part of the function with the same, and, as the documentation says, this function “performs a series of checks to ensure the current user can create topics“. The function returns a boolean value – true or false.
Basically, we can add an additional step to this validation process and change the returning value of this function by checking how many topics a user has already created.
Let’s use the function we created in the previous step to achieve this goal:
add_filter( 'bbp_current_user_can_access_create_topic_form', 'limit_topics_creation_per_day', 10, 1 );
function limit_topics_creation_per_day( $can_create ) {
// If a user can not create topics, no additional validations required
if ( ! $can_create ) {
return $can_create;
}
// If a user is a moderator or keymaster, they can create as many topics as they want
if ( current_user_can( 'moderate' ) ) {
return $can_create;
}
$user_id = get_current_user_id();
$user_topics_started_today = get_user_topics_started_today( $user_id );
if ( $user_topics_started_today >= 5 ) {
return false;
}
return true;
}
Now, if I log in as an Anticipator and create five topics, I won’t be able to create any new topics today. That’s what I will see instead of the topic creation form:
Summary
The code examples from this post can be easily extended and modified to fit your specific goals. For example, you can count topics of the specific forum by specifying the post_parent argument in your WP_Query arguments.
However, I hope you got the basic idea and found this post helpful.
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!