Limiting BuddyPress Activity Posting by User Role with Daily and Weekly Limits
Purpose
This code snippet is designed to limit the number of activity posts that users can make in BuddyPress based on their roles. It allows you to set daily and weekly limits on how many posts a user can make.
How It Works
The snippet performs the following tasks:
- Restricts activity posting: Users can only post a certain number of activity updates per day and week, which can be configured via the code.
- Hides the post form: Once the user has reached their limit, the “What’s New” form is hidden, and a notification is displayed.
How to Use
- Add the Code Snippet:
- Copy the following code and add it to your theme’s
functions.phpfile or use a custom functionality plugin to apply this to your WordPress/BuddyPress site.
- Copy the following code and add it to your theme’s
/**
* Hide BuddyPress Activity Post form based on daily and weekly limits.
*/
function blpro_hide_user_activity_posting( $templates, $slug, $name ) {
if ( 'activity/post-form' != $slug ) {
return $templates;
}
if ( is_user_logged_in() ) {
global $current_user;
// Get User Restrictions.
$blpro_membership_restrictions = bp_get_option( 'blpro_membership_restrictions' );
$user_role = $current_user->roles;
$user_id = get_current_user_id();
// Check if role has activity form restriction
if ( ! empty( $user_role ) ) {
foreach ( $user_role as $role ) {
$activity = ( ! empty( $blpro_membership_restrictions[ $role ]['activity'] ) ) ? $blpro_membership_restrictions[ $role ]['activity'] : array();
// If activity form is restricted for this role, hide the form.
if ( isset( $activity['form'] ) && $activity['form'] == 'yes' ) {
$templates = array(); // Empty the template, hiding the form.
break;
}
// Check post limits for roles that are not restricted from posting.
$post_count = blpro_get_user_activity_count( $user_id );
// Set limits (e.g., 10 posts per day, 50 posts per week)
$daily_limit = 10;
$weekly_limit = 50;
// Check if the user exceeded daily or weekly limits
if ( $post_count['daily'] >= $daily_limit || $post_count['weekly'] >= $weekly_limit ) {
$templates = array(); // Empty the template, hiding the form.
break;
}
}
}
}
return $templates;
}
add_filter( 'bp_get_template_part', 'blpro_hide_user_activity_posting', 10, 3 );
/**
* Get the current user's activity post count for the day and week.
*/
function blpro_get_user_activity_count( $user_id ) {
$today = date( 'Y-m-d' );
$week_start = date( 'Y-m-d', strtotime( 'monday this week' ) );
$week_end = date( 'Y-m-d', strtotime( 'sunday this week' ) );
global $wpdb;
$activity_table = buddypress()->activity->table_name;
// Get today's post count
$daily_query = $wpdb->prepare(
"SELECT COUNT(*) FROM $activity_table WHERE user_id = %d AND date_recorded BETWEEN %s AND %s",
$user_id,
$today . ' 00:00:00',
$today . ' 23:59:59'
);
$daily_count = $wpdb->get_var( $daily_query );
// Get this week's post count
$weekly_query = $wpdb->prepare(
"SELECT COUNT(*) FROM $activity_table WHERE user_id = %d AND date_recorded BETWEEN %s AND %s",
$user_id,
$week_start . ' 00:00:00',
$week_end . ' 23:59:59'
);
$weekly_count = $wpdb->get_var( $weekly_query );
return array(
'daily' => $daily_count,
'weekly' => $weekly_count,
);
}
/**
* Hide the activity post form after each post if the user has exceeded the daily/weekly limit.
*/
function blpro_after_activity_posted( $activity ) {
$user_id = get_current_user_id();
// Check post count after activity is posted
$post_count = blpro_get_user_activity_count( $user_id );
// Set limits
$daily_limit = 10;
$weekly_limit = 50;
// If post limits are reached, enqueue a script to hide the activity post form
if ( $post_count['daily'] >= $daily_limit || $post_count['weekly'] >= $weekly_limit ) {
?>
- Configure the Limits:
- To modify the daily and weekly post limits, update the
$daily_limitand$weekly_limitvariables inside the code:- Daily Limit:
$daily_limit = 10;// Users can post 10 times per day. - Weekly Limit:
$weekly_limit = 50;// Users can post 50 times per week.
- Daily Limit:
- To modify the daily and weekly post limits, update the
This snippet is a quick and effective way to control how many activities users can post on BuddyPress. It helps prevent spammy behavior and controls the flow of activity within your community.
