add_action( 'messages_message_before_save', 'mycred_pro_intercept_bp_message' ); function mycred_pro_intercept_bp_message( $message ) { // We need recipients and myCRED if ( empty( $message->recipients ) || ! function_exists( 'mycred_get_users_rank' ) ) return; // The message sender $cui = bp_loggedin_user_id(); // Get the senders Rank ID so we can get the ranks minimum balance requirement $senders_rank_id = mycred_get_users_rank( $cui, 'ID' ); $senders_min = get_post_meta( $senders_rank_id, 'mycred_rank_min', true ); // Now we loop through the recipients $good_recipients = array(); foreach ( $message->recipients as $recipient ) { // Get the recipients rank ID so we can get the rank minimum balance requirement $recipients_rank_id = mycred_get_users_rank( $recipient->user_id, 'ID' ); $recipients_min = get_post_meta( $recipients_rank_id, 'mycred_rank_min', true ); // If recipient is on a higher rank, continue to the next if ( $recipients_min > $senders_min ) continue; // Add recipient to the "good" list $good_recipients[] = $recipient; } // Replace set recipients with the ones we have approved // If this results in an empty array (all recipients declined) // BuddyPress will show an error to the user and no message is sent. $message->recipients = $good_recipients; }