Iam working with buddypress,
I have a two user roles,
1-student
2-faculty
and i have set default user role as subscriber.
when user registers and activates account by clicking on link sent through mail.User role changes to default(subscriber).
Any idea what is the issue? Below is the code assigning role to user on sign up.
add_action('bp_core_signup_user', 'ad_user_signup_usermeta', 10, 5);
function ad_user_signup_usermeta($user_id, $user_login, $user_password, $user_email, $usermeta) {
if(isset($_POST['signup_membership']) && !empty($_POST['signup_membership']))
update_user_meta($user_id, 'membership', $_POST['signup_membership']);
$userdata = array();
$userdata['ID'] = $user_id;
if(!empty($_POST['signup_usertype'])) {
if($_POST['signup_usertype'] == 'student') {
$userdata['role'] = 'student';
}
if($_POST['signup_usertype'] == 'instructor') {
$userdata['role'] = 'instructor';
}
}
if ($userdata['role']){
wp_update_user($userdata);
}
}
Upon activation, BuddyPress (at least version 2.0.2) updates the user's role to the default role.
https://buddypress.trac.wordpress.org/browser/tags/2.0.2/bp-members/bp-members-functions.php#L1560
You can comment out that line, or write some code to work around it. I'm using "WP Roles At Registration" and ran across the same problem. I ended up adding a filter on bp_core_signup_user to save the original role but you'll want to add something like this to your ad_user_signup_usermeta:
update_user_meta($user_id, 'temp_role', $role_name)
then reset it back in a filter for bp_core_activated_user
public function after_bp_activated_user($user_id, $key, $user) {
$user = get_userdata($user_id);
$role = get_user_meta($user_id, 'temp_role');
if ($role) {
$user->set_role($role[0]);
}
}
add_filter('bp_core_activated_user', array($this, 'after_bp_activated_user'), 30, 3);
Related
I want to be able to manually confirm offers --> Thereby changing the order status before a user gets assigned a new role on my page. I tried to combine two functions I found here but when I launched them the page crashed.
The manual confirmation is important since we have to check each person that wants to purchase a membership. The Roles are important because I use them to limit access to other parts of the website.
A users Journey:
A user comes to the website. Then has to log in or create a profile. Then they are forwarded to the site where you can get the membership. (Woocomerce and Stripe) I want to be able to manually confirm if somebody buys a membership or not. If I confirm the user gets a new role. If I don't confirm the user gets no role or is deleted ( the payments should obviously not happen in that case.
What is the problem can somebody please help?
function uiwc_change_role( $order_id ) {
// get all the order data
$order = new WC_Order($order_id);
$user = $order->get_user();
$order_status = $order->get_status();
if ('complete' == $order_status) {
if( false != $user && !user_can($user, 'administrator') ){
// our new role name
$role = 'aktives_mitglied2022';
//set the new role to our customer
$user->set_role($role);
}
//return $order_status;
}
}
//add this newly created function to the thank you page
add_action( 'woocommerce_thankyou', 'uiwc_change_role', 100, 1 );
function custom_uiwc_change_role( $order_id ) {
// get all the order data
$order = new WC_Order($order_id);
$user = $order->get_user();
$order_status = $order->get_status();
if ('complete' == $order_status) {
if( false != $user && !user_can($user, 'administrator') ){
// our new role name
$role = 'aktives_mitglied2022';
//set the new role to our customer
$user->set_role($role);
}
//return $order_status;
}
}
//add this newly created function to the thank you page
add_action( 'woocommerce_thankyou', 'custom_uiwc_change_role', 100, 1 );
I made a plugin that requires the user to validate their email address. It creates "activation_key" meta key with random string for the user and sets it to '' once the user validates. So far so good. But now I need to hook into login and check that activation_key == ''.
This is what I thought should be, but it doesn't get to here.
add_filter( 'authenticate', 'check_if_activated', 10, 3 );
function check_if_activated($user, $username, $password)
{
// check if user has activated their email
return $user;
}
Wordpress already adds filters to authenticate, so you have to register your lower.
add_filter( 'authenticate', 'check_if_activated', 50);
function check_if_activated($user)
{
// If we have an error, no need to check the activation key
// (Wrong credentials, for instance)
if (is_wp_error($user))
{
return $user;
}
// Checks the meta and returns an error if needed
$validation_key = get_user_meta($user->ID, 'activation_key', true);
return empty($validation_key) ? $user : new WP_Error('your_plugin_error_code', 'your error message');
}
I have added custom fields on registration to allow the user to input their own password and have also created code to generate a verification code which then gets emailed over to the user. Of course, the users needs to click the link in the email before they can log in.
Here's where I am stuck. I am trying to add my own authentication to check the status of the verification when the user tried to log in.
Here's my code which isn't working;
function check_validation_status($username) {
$user = get_user_by('login', $username);
$userID = $user->ID;
$status = get_user_meta($userID, 'verified', true);
if($status == '0') {
return;
}
}
add_action('wp_authenticate', 'check_validation_status');
Unfortunately this code doesn't seem to do anything. I have also tried the following (hooking into a different action)
function check_validation_status($username) {
$user = get_user_by('login', $username);
$userID = $user->ID;
$status = get_user_meta($userID, 'verified', true);
if($status == '0') {
wp_logout(); // works but doesn't show an error :(
}
}
add_action('wp_login', 'check_validation_status');
This code is successfulling logging the user straight out if they are not verified however it shows no form of error to the user, they just get redirected straight back to the login page.
Logging the user in but straight back out seems like a sloppy way to do it, is there a way to prevent the log in in the first place?
I have managed to fix this issue now. I instead needed to hook into wp_authenticate_user and return a WP_Error. Here is my working code, I hope it helps someone out in the future.
function check_validation_status($user, $password) {
$userID = $user->ID;
$status = get_user_meta($userID, 'verified', true);
if($status == '0') {
$errors = new WP_Error();
$errors->add('title_error', __('<strong>ERROR</strong>: This account has not been verified.', 'podium'));
return $errors;
}
return $user;
}
add_action('wp_authenticate_user', 'check_validation_status', 10, 2);
I need to grant users a specific role (Editor, Administrator, etc.) along with all its capabilities on the fly in Wordpress, but I don't want to update their role in the database (so that the next time they come back, they will have their original role). How can I go about doing this?
Here's what I ended up doing:
add_filter( 'user_has_cap', 'override_caps' );
function override_caps($allcaps){
if( ... ){ // When to override caps
$role_name = 'administrator';
$role = get_role($role_name); // Get the role object by role name
$allcaps = $role->capabilities; // Get the capabilities for the role
$allcaps[$role_name] = true; // Add role name to capabilities
}
return $allcaps;
}
I use buddypress with multiple groups. Every user can also join multiple groups as a member. I use custom profile fields for e.g. the address. Every member can select, who is able to view his profile. This try is based on the plugin 'User Profile Visibility Manager'. Now I need an additional option: View the profile to "Group Members Only"
My plan is:
Get the user_id of the 'visitor' - works perfect with:
$user_id_visitor = bp_loggedin_user_id();
Get the user_id of the 'profile owner' - works perfect with:
$user_id_profile_owner = bp_displayed_user_id();
Get the group(s) by the profile owner´s user_id:
Here, I´ve tried a lot. With this function, I´m able to print all groups in which the 'profile owner' is a member. But I don´t need to print it, it is only for a test:
function bp_groups_profileowner( $user_id_profile_owner ) {
global $wpdb;
$groups = $wpdb->get_results( $wpdb->prepare( "SELECT group_id FROM wp_bp_groups_members WHERE user_id = %d", $user_id_profile_owner ) );
if ( $groups ) {
foreach ( $groups as $group ) {
echo '<li>' . $group->group_id . '</li>';
}
}
check all members of the profile_owner´s group(s) and check, if the visitor is also member the group(s).
My new option in the selectbox:
<option value="groupmembers" <?php echo selected('groupmembers',bp_profile_visibility_get_settings($user_id,'bp_profile_visibility' ));?>><?php _e('Group Members Only','bp-profile-visibility');?></option>
This is the code snippet, coming from the plugin, which checks and protects user account visibility:
/**
* Checks and protects user account visibility
* #return type
*/
function check_profile_access(){
if(!bp_is_user() || is_super_admin())
return;
//if we are on user profile page
$privacy = bp_profile_visibility_get_settings(bp_displayed_user_id(), 'bp_profile_visibility');
//if privacy is public, everyone can see
if( 'public' == $privacy )
return;
$referrer=wp_get_referer();
if($referrer)
$referrer=bp_core_get_root_domain ();
//in all other cases, user must be logged in
if(!is_user_logged_in()){
bp_core_add_message(__('Please login to view profile','bp-profile-visibility'),'error');
wp_safe_redirect($referrer);
exit(0);
return ;
}
//if we are here, the person is logged in, let us see if the visibility is set to logged in
if( 'loggedin' == $privacy )
return ;
//if this is my profile, do not prevent user
if(bp_is_my_profile())
return ;
//now, since we have already tested for login , we just need to test for the friends only and me
if( 'friends' == $privacy && function_exists('friends_check_friendship') && friends_check_friendship(bp_displayed_user_id(), get_current_user_id()) )
return;
//now, we just need to test for the group members
if( 'groupmembers' ... )
//if we are here, don't show the profile
bp_core_add_message(__('This User\'s privacy settings does not allow you to view the profile.','bp-profile-visibility'),'error');
wp_safe_redirect($referrer);
exit(0);
return ;
}
I found the solution by myself:
//now, we just need to test for the group members only and me
if( 'groupmembers' == $privacy )
/**
* Check if visitor is a member of one of the profile owners groups mm
*
*/
$user_id_visitor = bp_loggedin_user_id();
$user_id_profile_owner = bp_displayed_user_id();
$all_groups = BP_Groups_Member::get_group_ids( $user_id_profile_owner);
if ( $all_groups ) {
foreach($all_groups[groups] AS $profile_owner_groups_id)
{
if (groups_is_user_member( $user_id_visitor, $profile_owner_groups_id ))
// break if visitor is member of one of the profie owners group //
return;
}
}