Skip to:
Content
Pages
Categories
Search
Top
Bottom

Resctrict private messages


  • talvasconcelos
    Participant

    @talvasconcelos

    Hi guys, need a little help with some custom action. I need to restrict private messages to paying members only, or if the message (thread) comes first from a paying user.

    I use Paid Membership Pro to manage the membership. I tried including this function in the theme’s function file:

    function message_from_non_premium($message_info) {
    	$user = get_current_user_id();
    	$premium = pmpro_hasMembershipLevel('1', $user);
    
    	if(!$premium) {
    		if(!thread_id) {
    			unset( $message_info->recipients );
    			return false;
    		}
    	}
    }
    
    add_action('messages_message_before_save', 'message_from_non_premium');

    but when testing, users can still send private messages, even without the required membership to do so. I used info from the forum and the pmp website: https://www.paidmembershipspro.com/documentation/content-controls/require-membership-function/

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

  • shanebp
    Moderator

    @shanebp

    What is thread_id? if(!thread_id) {
    Try removing that conditional.

    Please do not double post.


    talvasconcelos
    Participant

    @talvasconcelos

    Hi, thread_id is from the buddypress. When a message is going for save bp checks if there’s a thread id, if not it’s because that is the first message.

    Anyway, i got it to kind of work. Used some info from the other thread. I use the add_filter() to check if the user has a membership and if not, take out the button. Also i was forgeting to include the file with the Paid membership function. Here’s the code:

    add_filter( 'bp_get_send_message_button', function( $array ) {
        if ( pmpro_hasMembershipLevel('Premium') ) {
            return $array;
        } else {
            return '';
        }
    } );

    If you have any recommendations for this i’ll be glad to follow.


    empressdianna
    Participant

    @empressdianna

    Thank you so much for this code! I’ve spent several hours trying to use other people’s code to hide the button and even tried to make my own but nothing worked until I found your code. I just switched it around because I only wanted one particular group to not be able to send messages while the rest can.

    Posting in case someone in the future needs the opposite of your code:

    add_filter( 'bp_get_send_message_button', function( $array ) {
        if ( pmpro_hasMembershipLevel('NAMEOFPMPLEVEL') ) {
            return '';
        } else {
            return $array;
        }
    } );

    Also, I am looking for the same code as you to restrict non-paying users from sending private messages. I found a code that looks promising but unfortunately doesn’t work. It sees if the user is logged in and if not redirects them. Then if the person is logged in it checks their role but if they’re not that role they’re supposed to be redirected. But it just breaks my whole site. Any ideas?

    function my_function() {
    
        if ( ! is_user_logged_in() && bp_is_current_component( 'messages' ) ) {
            // Current user is not logged in so don't let them see stuff. In this case, private messages 
            wp_redirect( home_url() );
            exit;
        }
            
        $flag = false;
        // Get current user info.
        $user = wp_get_current_user();
    
        if ( ! in_array( 'student', $user->roles ) ) {
            // The current member is not a student.
            $flag = true;
        }
    
        if ( $flag && bp_is_current_component( 'messages' )  ) {
            The currently logged-in member is not a student and is trying to view private messaging. Let's redirect them away.
            wp_redirect( home_url() );
            exit;
        }
    }
    add_action( 'init', 'my_function' );

    empressdianna
    Participant

    @empressdianna

    Found this on another forum and after some tweaking finally got this to work. This will only work if you force all new and existing non-paying users into one group however without some further tweaking. If a non-paying user tries to click on the “messaging” tab in wordpress it will redirect them to any page you choose. I chose the memberships level page. =D

    function bp_disable_messaging() {
    	global $bp;
    	
    	if ( pmpro_hasMembershipLevel('NonPayingLevel') && bp_is_current_component('messages') ) {
    		wp_redirect( site_url("/PAGE URL/") );
    		exit();
    	}
    }
    add_action('wp','bp_disable_messaging');

    empressdianna
    Participant

    @empressdianna

    Now if someone can help me send a generic notification message instead of the whole message that would be great….


    danbp
    Moderator

    @danbp

    @empressdianna,

    please use the code button to insert code into topics ! Thx.

    You use PMPro and share code related to it. That’s nice and kind, but it is probably more difficult for the most of use who don’t use that plugin to discuss with you.
    It can help you, here a great ressource to find anything inside BuddyPress: ie. search result for “N”

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