Skip to content

Instantly share code, notes, and snippets.

@ideadude
Created October 12, 2020 20:31
Show Gist options
  • Save ideadude/282de6c893817da10c5f3727b427de85 to your computer and use it in GitHub Desktop.
Save ideadude/282de6c893817da10c5f3727b427de85 to your computer and use it in GitHub Desktop.
Only let certain members post BuddyPress activity when using PMPro and PMPro BuddyPress.
/**
* Only let certain members post BuddyPress activity.
* This code works with BuddyPress and BuddyBoss.
* This code only filters the main activity page.
* Posting to groups you are a member of still works.
*
* Make sure BuddyPress (or BuddyBoss), PMPro, and PMPro BuddyPress are all active.
* Copy this code into a custom plugin or Code Snippet.
*/
// Define the levels that CAN post activity here.
define( 'MY_LEVELS_WITH_ACTIVITY', array(1,4,5) );
// JS to disable the activity form and show error
function my_hide_bp_activity_form() {
// Bail if no PMPro or the current user is a paying member
if ( ! function_exists( 'pmpro_hasMembershipLevel' ) || pmpro_hasMembershipLevel( MY_LEVELS_WITH_ACTIVITY ) ) {
return;
}
// Bail if we're on a group page.
if ( bp_is_group() ) {
return;
}
// Change the text here. Add a link to your levels page.
?>
<script>
jQuery(document).ready(function() {
jQuery('#whats-new-textarea').html('<p>You must have a paying membership to post activity.</p>');
});
</script>
<?php
}
add_action( 'bp_after_activity_post_form', 'my_hide_bp_activity_form' );
// Filter to stop activity from posting.
function my_bp_activity_filter( $activity ) {
if ( ! function_exists( 'pmpro_hasMembershipLevel' ) ) {
return $activity;
}
if ( $activity->component == 'activity' && ! pmpro_hasMembershipLevel( MY_LEVELS_WITH_ACTIVITY ) ) {
$activity->error_type = 'wp_error';
$activity->errors->add( 'pmpro_bp_membership_required', 'Only certain members may post activity.' );
}
return $activity;
}
add_filter( 'bp_activity_before_save', 'my_bp_activity_filter' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment