Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active February 3, 2020 09:22
Show Gist options
  • Select an option

  • Save igorbenic/ee23b34e182446b186826a8783fc714d to your computer and use it in GitHub Desktop.

Select an option

Save igorbenic/ee23b34e182446b186826a8783fc714d to your computer and use it in GitHub Desktop.
Controlling BuddyPress Private Messages
<?php
/**
* Plugin Name: Control Private Messages in BuddyPress
* Author: Igor Benic
* Author URI: http://www.ibenic.com
* Version: 1.0
*/
if( ! defined( 'ABSPATH' ) ) {
return;
}
if( class_exists( 'BuddyPress' ) ) {
// We are sure that BuddyPress has been activated.
}
<?php
// ...
add_filter( 'bp_get_send_message_button', 'control_private_message_button' );
/**
* Returns HTML for the Private Message Button or false
* @param string $html
* @return mixed
*/
function control_private_message_button( $html ) {
if( ! this_user_can_create_private_messages() ) {
return false;
}
return $html;
}
<?php
// ...
add_filter( 'messages_template_compose', 'control_display_of_message_form' );
/**
* Removes the Form if can't use it
* @param string $template
* @return mixed
*/
function control_display_of_message_form( $template ) {
if( ! this_user_can_create_private_messages() ) {
return false;
}
return $template;
}
<?php
// ...
add_action( 'messages_message_before_save', 'control_sending_of_private_message' );
/**
* Control the message from Private Messages before beign saved/sent
* @param BP_Messages_Message $message_object
* @return void
*/
function control_sending_of_private_message( $message_object ) {
$sender_id = $message_object->sender_id;
if( ! user_can_create_private_messages( $sender_id ) ) {
$message_object->recipients = false;
}
}
<?php
// ...
add_action( 'bp_messages_setup_nav', 'control_parts_of_private_messages' );
/**
* Control Parts of Private Messages
* @return void
*/
function control_parts_of_private_messages() {
if( ! this_user_can_create_private_messages() ) {
// Remove Compose
bp_core_remove_subnav_item( 'messages', 'compose' );
}
if( ! this_user_can_see_sent_private_messages() ) {
// Remove Sent
bp_core_remove_subnav_item( 'messages', 'sent' );
}
if( ! this_user_can_see_sent_private_messages() ) {
// Remove Sent
bp_core_remove_subnav_item( 'messages', 'sent' );
}
if( ! this_user_can_see_starred_private_messages() ) {
// Remove Starred
bp_core_remove_subnav_item( 'messages', bp_get_messages_starred_slug() );
}
if( ! this_user_can_use_messages() ) {
bp_core_remove_nav_item( bp_get_messages_slug() );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment