Change Wordpress user role on gravityforms submission - php

I am using Gravityforms along with User registration add-on and have a form which when submitted should change the role of the current user to a new role without any underlying conditions.
Using the gravity\forms docs https://docs.gravityforms.com/gform_user_updated/#1-update-user-role and trying this:
add_action( 'gform_user_updated_3', 'change_role', 10, 3 );
function change_role( $user_id, $feed, $entry, $user_pass ) {
global $current_user;
get_currentuserinfo();
$user_id = $current_user->ID;
echo $user_id;
if( ! $user_id ) {
return;
}
$user = new WP_User( $user_id );
$user->set_role( 'role' ); // update 'role' to the name of the desired role
}
But its not working! Does anyone have any idea why this is incorrect or any other modifications to the code?

There's a couple things I see with your code when comparing it to the Gravity Forms doc.
Here's your code with some added comments:
add_action( 'gform_user_updated_3', 'change_role', 10, 3 );
function change_role( $user_id, $feed, $entry, $user_pass ) {
global $current_user; // you probably don't need this
get_currentuserinfo(); // you probably don't need this
$user_id = $current_user->ID; // $user_id should already be a numeric value passed in to the function containing the logged in user's ID so you shouldn't need to do this. You're resetting the $user_id variable here to whatever is being pulled out of the get_currentuserinfo() function, and I'm guessing that's the problem
//I would get rid of this echo and if statement
echo $user_id;
if( ! $user_id ) {
return;
}
$user = new WP_User( $user_id );
$user->set_role( 'role' ); // the word "role" here needs to be the role name
}
I think you can simplify it some. Try this instead:
add_action( 'gform_user_updated_3', 'change_role', 10, 3 );
function change_role( $user_id, $feed, $entry, $user_pass ) {
$user = new WP_User( $user_id );
$user->set_role( 'new_role_name_here' ); // Add an existing role here to update the user too
}

Related

How To Set global current_user on Wordpress Using PHP?

I am using the following code to login a user.
$UserName and $Password is coming in via a Form. The following if statement is showing TRUE, so my user is getting logged in.
$user = get_user_by( 'login', $UserName );
if ( $user && wp_check_password( $Password, $user->data->user_pass, $user->ID) )
Now as long as the User is on the website I want his $UserName to be known by all the PHP programs he invokes.
Whenever I log someone in using the wp-admin screen the $UserName is brought in to all PHP programs by executing the following code:
global $current_user;
if ( isset($current_user) ) {
$UserName=$current_user->user_login;
}
My question is.... using the login at the top of this post how do I save the $Username in such a way that it is brought into other PHP programs using the above isset($current_user) if statement?
I tried the following code but it does not bring the $current_user field into other PHP programs by executing the IF (isset($current_user) statement. The $user->user_login field contents is equal to $UserName.:
$user_id = $UserName;
$user = get_user_by( 'id', $user_id );
$current_user= new WP_User( $user_id , $user->user_login );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $user->user_login );
global $current_user;
What am I missing? Any assistance will be greatly appreciated. Thank you

WordPress - remove capabilities based on user id

I current have this code written:
add_action( 'init', 'hide_user_caps' );
function hide_user_caps() {
$user_id = 1;
$user = new WP_User( $user_id );
$user->remove_cap( 'edit_posts' );
}
However, this doesn't seem to be doing anything. Any ideas?
Thank you
If you want to specifically deny a capability from a user (not a role), you need to use the add_cap()[1] method and specify that the $grant parameter is false. [2].
$user_id = 1;
$user = new WP_User( $user_id );
$user->add_cap( 'edit_posts', false );
[1]: WP_User::add_cap()
[2]: The answer

Contact Form 7 (CF7) radio button to change User Role on WordPress

I have a form setup for users to fill in after they register on my WordPress site. Ive setup the form using Contact Form 7, and have a radio button called radio-766 that has two options: subscriber and customer.
I want the user to pick one of these two options, then when they submit the form, it will change their user role.
Below is what I have so far... I've grabbed snippets from online and tried to create my own, but it isn't working. Any ideas on how I can get this to work?
function tm_cf7_roles_posted_data( $posted_data ) {
// Stop if user is not logged in.
if ( ! is_user_logged_in() )
return;
ob_start();
$role = sanitize_key( $posted_data['radio-766'] );
if ( ! in_array( $role, array( 'subscriber', 'customer' ) ) )
return;
$user = new WP_User( get_current_user_id() );
$index = key( $user->roles );
$user_role = $user->roles[ $index ];
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_action( 'wpcf7_posted_data', 'tm_cf7_roles_posted_data' );
Should I be including the form name or ID anywhere? Can't find info on this
Any help is so appreciated!
EDIT
I feel like there is nothing connecting this function to the CF7 form named "After LinkedIn", so I found this snippet of code, just not sure how to integrate and get working
if (!isset($cfdata->posted_data) && class_exists('WPCF7_Submission')) {
// Contact Form 7 version 3.9 removed $cfdata->posted_data and now
// we have to retrieve it from an API
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$formdata = $submission->get_posted_data();
}
} elseif (isset($cfdata->posted_data)) {
// For pre-3.9 versions of Contact Form 7
$formdata = $cfdata->posted_data;
} else {
// We can't retrieve the form data
return $cfdata;
}
// Check this is the user registration form
if ( $cfdata->title() == 'After LinkedIn') {
As per the Contact form 7 plugin author is_user_logged_in() will work on below to cases in form submission:
subscribers_only: true set in the additional setting in form. OR
Set WPCF7_VERIFY_NONCE to true using add_filter( 'wpcf7_verify_nonce', '__return_true' );
For more information click here.
Also, to change the user role you can do the following:
add_action( 'wpcf7_posted_data', 'tm_cf7_roles_posted_data' );
function tm_cf7_roles_posted_data( $posted_data ) {
$submission = WPCF7_Submission::get_instance();
$wpcf7 = WPCF7_ContactForm::get_current();
$formID = $wpcf7->id();
if ( $submission ) {
if( $formID == "YOUR-FORM-ID" ) {
if ( is_user_logged_in() ) {
$role = sanitize_key( $posted_data['radio-766'][0] );
if ( ! in_array( $role, array( 'subscriber', 'customer' ) ) )
return;
// Getting the WP_User object
$user = wp_get_current_user();
// The user exists
if( $user && $user->exists() ) {
// Check if user already has that role or not
if ( !in_array( $role, (array) $user->roles ) ) {
// Remove all the previous roles from the user and add this one
// This will also reset all the caps and set them for the new role
$user->set_role( $role );
}
}
}
}
}
}
If you only want to add a new role to the user and retain the existing role then instead of set_role use below:
// Add a new role to the user while retaining the previous ones
$user->add_role( $role );
As for programatically setting a user role you can just take the user object and use the set_role() function to change their role to whatever you want as long as that role has been defined.Try this way
function tm_cf7_roles_posted_data( $posted_data ) {
// Stop if user is not logged in.
if ( ! is_user_logged_in() )
return;
ob_start();
$role = sanitize_key( $_POST['radio-766'] );
if ( ! in_array( $role, array( 'subscriber', 'customer' ) ) )
return;
$user = new WP_User( get_current_user_id() );
$index = key( $user->roles );
$user_role = $user->set_role($role);
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_action( 'wpcf7_posted_data', 'tm_cf7_roles_posted_data' );

Change role of all users with email address from specific domain upon custom WordPress plugin activation

I'd like to change the role of all users with a specific domain name (#example.com) to a custom role when my plugin is activated.
I found some code for doing it when a user registers and attempted to adapt it to my needs but it doesn't seem to work. Nothing happens when I activate the plugin. The roles don't change and I don't get any errors popping up so I'm not quite sure what I'm doing wrong.
I'm still learning PHP so please forgive me if this doesn't make sense.
Here's my code:
function set_role_by_email( $user_id ){
$user = get_user_by( 'id', $user_id );
$domain = substr(
strrchr(
$user->data->user_email,
"#"
), 1
); //Get Domain
$custom_role_domains = array( 'example.com' );
if( in_array( $domain, $custom_role_domains ) ){
foreach( $user->roles as $role )
$user->remove_role( $role ); //Remove existing Roles
$user->add_role( 'custom_role' ); //Add role to user
}
}
register_activation_hook( __FILE__, 'set_role_by_email' );
You could try this code and see if it works:
function set_role_by_email()
{
$users = get_users();
foreach ($users as $user) {
if (strpos($user->user_email, '#example.com')) {
foreach ($user->roles as $role) {
$user->remove_role($role);
}
$user->add_role('custom_role');
}
}
}
register_activation_hook(__FILE__, 'set_role_by_email');
Make sure this code is placed in the main plugin file.

WP: How can I show all user_meta fields?

<?php
global $current_user;
get_currentuserinfo();
$user_id = $current_user->ID;
$user_fields = get_user_meta( $user_id, , 'true' );
echo $user_fields;
?>
It showing 'Array'. I would want to know the field to be used on counting the number of times the user logins.
use:
var_dump($user_fields)
It would show you the variable content !
use
$user_fields = get_user_meta( $user_id );
print_r($user_fields);
it will showing all meta field regarding $user_id
In wordpress there is no any pre-defined functionality is available
for get user login count.. You have to options for doing this..
either create a plugin or write the follwing custom code on
functions.php file..
Here I have create a variable user_login_count in user meta key in regards to user id, so everytime user successfully login, i m increasing the user count by 1..
function ulc_my_login_redirect($redirect_to, $request, $user) {
//is there a user to check?
global $user, $wpdb;
if (isset($user->ID)) {
if ( get_user_meta($user->ID, 'user_login_count', true) ){
$user_login_count = get_user_meta($user->ID, 'user_login_count', true);
$user_login_count++;
update_user_meta($user->ID, 'user_login_count', $user_login_count);
}else{
add_user_meta($user->ID, 'user_login_count', '1');
}
}
return $redirect_to;
}
add_filter('login_redirect', 'ulc_my_login_redirect', 10, 3);
By using this filter you can get the key value of 'user_login_count' user get_post_meta function..

Categories