Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@imath
Last active May 3, 2023 06:24
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save imath/4703845 to your computer and use it in GitHub Desktop.
Save imath/4703845 to your computer and use it in GitHub Desktop.
I was writing my second BuddyPress tutorial for wpformation.com when i realized it could be helpful for a community admin to restrict private messages to friends only...
<?php
/*** beginning of the code to paste in your functions.php ***/
function imath_pm_button_only_if_friends( $button ) {
if( is_super_admin() )
return $button;
if( 'is_friend' != friends_check_friendship_status( bp_displayed_user_id(), bp_loggedin_user_id() ) )
return false;
else
return $button;
}
add_filter( 'bp_get_send_message_button', 'imath_pm_button_only_if_friends', 10, 1 );
function imath_fillfield_only_if_friends() {
if( empty( $_GET['r'] ) )
return;
if( is_super_admin() )
return;
$path = esc_url( $_SERVER['REQUEST_URI'] );
$redirect = bp_core_get_root_domain() . $_SERVER['REDIRECT_URL'];
preg_match('/[\?r] *= *["\']?([^"\' ]*)[\& ]/is', $path, $match );
if( !empty( $match[1] ) ) {
$user_id = bp_core_get_userid( $match[1] );
if( 'is_friend' != friends_check_friendship_status( $user_id, bp_loggedin_user_id() ) )
bp_core_redirect( $redirect );
}
}
add_action( 'messages_screen_compose', 'imath_fillfield_only_if_friends' );
/*** end of the code to paste in your functions.php ***/
?>
@georgio-1
Copy link

georgio-1 commented Feb 28, 2017

Hi @imath
I tested the gist. The second part of the code doesn't work for me. A member can always send a message to a non-friend from the messages tab on the bar.

@AmauryVanEspen
Copy link

Great Feature,
could you let me know how could i rename "friend" by "relationship" ? (for functional purpose @imath )
thank you

@imath
Copy link
Author

imath commented May 3, 2023

Hi, the best way to achieve this is to use custom translation files and put the po/mo files inside a new WP_LANG_DIR . '/buddypress folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment