Skip to:
Content
Pages
Categories
Search
Top
Bottom

Private Message Friends Only


  • @mcuk
    Participant

    @mcuk

    Read a number of posts on this topic. Just wondered if there are any plans in BuddyPress development to include the option of making private messaging between friends only and prevent users PM’ing anybody and everybody. Reasons being: 1. To reduce spamming, therefore not annoying users who will then enjoy using the site more & 2. reduced requests on servers and possibly getting blacklisted.

    Are plugins the only route to accomplish this at the moment? If so any recommendations?

    Many thanks

Viewing 12 replies - 1 through 12 (of 12 total)

  • shanebp
    Moderator

    @shanebp

    afaik, there are no plugins.
    But it’s a simple filter.
    https://buddypress.org/support/topic/disable-or-hide-private-message-button/


    @mcuk
    Participant

    @mcuk

    Thanks for response Shane. I did see that post, but even with button removed it still allows a user to compose a message and send to someone who isn’t a friend, hence why i was still searching.

    I have already introduced a text limit on the compose field to discourage spamming, but it is only Javascript. So with Javascript turned off it is still possible to send big messages. Looking for the PHP validation database solution.

    Guess I’ll weigh up the value of PM’s on my site (not live atm) against the possible annoyance of spam to users. Better to introduce PMs at a later date rather than have it initially, find a problem with spam annoying people in the community and then having to remove the feature.


    neijisly
    Participant

    @neijisly

    Hi, this is the function code for message only friends

    <?php
    /*** beginning of the code to paste in your functions.php ***/
    function neiji_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’,’neiji_pm_button_only_if_friends’,10,1);

    function neiji_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’,’neiji_fillfield_only_if_friends’);
    /*** end of the code to paste in your functions.php ***/
    ?>


    @mcuk
    Participant

    @mcuk

    Hi @neijisly ,

    Thank you for your response. I tried the function neiji_fillfield_only_if_friends() you posted above with a minor amendments to the last if statement condition (due to the ‘is_friend’ which i assume is something else you have in your functions.php?), but no luck. (I already have a function in place similar to your first remove button function).

    From what i understand of your second function , it is meant to prevent the Message > Compose page loading and redirect if the users are not friends? (Correct me if I’m wrong). The issue I am having though is that when I am already on the Message > Compose page, I am still able to type in someone’s username and send a message to them even though they aren’t a friend.


    neijisly
    Participant

    @neijisly

    Try to replace with this

    function neiji_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’,’neiji_fillfield_only_if_friends’ );


    shanebp
    Moderator

    @shanebp

    Please use the code button when sharing code.

    This should work – but could cause conflicts with other plugins.
    And the message is the default fail message.
    So you should at least hard-code a warning into the compose template.

    // if not site admin, restrict PMs to friends
    function pp_check_message_recipients( $message_info ) {
    
    	//  site admins are not restricted
    	if ( is_super_admin() )
    		return $message_info;
    	
    	$recipients = $message_info->recipients;
    
    	$friend_ids = friends_get_friend_user_ids( bp_displayed_user_id() ); 
    
    	$nf = 0; 
    		
    	foreach ( $recipients as $key => $recipient ) {
    
    		if ( ! in_array( $recipient->user_id, $friend_ids ) ) 
    			$nf++;
    
    	}
    
    	// if any recipients are not friends, remove everyone from the recipient's list
    	if (  $nf > 0 ) 
    		unset( $message_info->recipients );
    
    	return $message_info;
    }
    add_action( 'messages_message_before_save', 'pp_check_message_recipients' );

    @mcuk
    Participant

    @mcuk

    Hi @neijisly, I tried your updated code but had no success with it. Thank you for trying though!

    Hi @shanebp, your code works great. Thank you! I don’t have any other plugins apart from BuddyBoss Global Search and BuddyBoss Edit Activity. So unless your code affects the functioning of another part of BuddyPress, then there shouldn’t be any conflicts.

    While looking at messaging I have just noticed that Private Messages don’t send when you select a recipient via the autocomplete dropdown (get a failed message saying username is incorrect). However if you manually type out the same username, the message sends fine. I haven’t yet searched the forum on this matter though so there may already be a solution out there. I’ll have a look.


    livingflame
    Participant

    @livingflame

    I want to add this function — Message Only for Friends — Like other Option for Users. How I can do?


    nomeansno
    Participant

    @nomeansno

    Forgive me, I’m a noob… Where am I supposed to copy/paste that code?

    Thanks for your help!


    Venutius
    Moderator

    @venutius

    You could put it in your functions.php or in a plugins/bp-custom.php file.


    nomeansno
    Participant

    @nomeansno

    Thanks a lot !


    david22c
    Participant

    @david22c

    The code above works to restrict private messages to to only friends, but the message the user gets if they try to message someone who is not a friend is:

    “The message was not sent. Please try again later.”

    Is there anyway to change this message to something like:

    “Sorry, you can only message connections within your network”

Viewing 12 replies - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.
Skip to toolbar