Solved – Hide private message button with specific conditions
-
Hello,
I am running a small blog site with mainly 3 roles on it . Subscriber, Contributor, Admin
I have been trying to remove the private message button under those conditions:– Hide message button from any users profile when Subscriber is logged in and viewing.
– Hide message bytton from all subscribers profile when Contributor & admin is logged in and viewing, BUT allow contributor & admin to view other contributors & admins msg buttons.I have tried something like this but didnt work:
function remove_private_message_button( $button ) { $user = bp_loggedin_user_id(); if ( !empty( $user->roles ) && is_array( $user->roles ) ) { if ( $role == 'subscriber') { $button = ''; } else if( $role == 'contributor') { if ( $user == 23 || $user == 6 || $user == 25 ) { // the allowed user ID's, lets say that the contributors & admins are few and i could add them manually by ID's return $button;} } } } add_filter( 'bp_get_send_message_button', 'remove_private_message_button', 1 , 1 );
any ideas would be appriciated
-
You cannot get roles from just the user id.
Try:function remove_private_message_button( $button ) { $user_id = bp_loggedin_user_id(); $user = get_userdata( $user_id ); $user_roles = $user->roles; $allowed_ids = array( 23, 6, 25 ); if ( !empty( $user_roles ) && is_array( $user_roles ) ) { if ( in_array( 'subscriber', $user_roles, true ) ) { $button = ''; } elseif ( in_array( 'contributor', $user_roles, true ) ) { if ( in_array( $user_id, $allowed_ids, true ) ) { return $button; } } } return $button; } add_filter( 'bp_get_send_message_button', 'remove_private_message_button', 1 , 1 );
Hello @shanebp,
Thank you so much for your help 🙂
I have tried your code (and also changed you lastreturn button;
withreturn $button;
), but it doesn’t work.If i login as subscriber all msg buttons are removed from all users profiles, which is correct.
If i login as contributor i can see msg buttons on contributors and admins profiles , but also on subscribers profiles, which i would like not to.*Lets assume that contributors and admins have ID 23, 6, 25 .
I tried many variations with your code but no luck.Thanks for pointing out the missing
$
– fixed.
Admins are not controlled by the function. They will always see the button.
You don’t need to check for$allowed_ids
.
This change should prevent contributors from seeing the button on subscriber profiles.elseif ( in_array( 'contributor', $user_roles, true ) ) { $displayed_user_id = bp_displayed_user_id(); $displayed_user = get_userdata( $displayed_user_id ); $displayed_user_roles = $displayed_user->roles; if ( in_array( 'subscriber', $displayed_user_roles, true ) ) { $button = ''; } }
Yes @shanebp, this works perfect. Thank you again for your help
Hello @shanebp , needed to inform you that i get an error with WordPress debug about the code above.
Trying to get property 'roles' of non-object
in line$displayed_user_roles = $displayed_user->roles;
i dont know why.
- You must be logged in to reply to this topic.