Skip Navigation

[Resolved] Change user role on CPT form submission

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted to change their user's role when they submit a post form.

Solution:
This can be done by using out cred_before_save_data hook along with the add_role() wordpress function.

Take a look at this link here.
https://usersinsights.com/wordpress-custom-role/

In a practical example it would look something like this.

add_action('cred_before_save_data_22', 'before_save_data_for_form_with_id_22',10,1);
function before_save_data_for_form_with_id_22($form_data)
{
if ( is_user_logged_in() ) {
if ($form_data['id']=='22')
{
$current_user = wp_get_current_user();
 $current_user->set_role('subscriber');
}
} else { }
}
This support ticket is created 5 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by andrei-laurentiuP 5 years, 2 months ago.

Assisted by: Shane.

Author
Posts
#1350107

Tell us what you are trying to do?
Change the role of the user after he submits a certain CPT form.

I have tried the following code:

add_action('cred_before_save_data_22', 'before_save_data_for_form_with_id_22',10,1);
function before_save_data_for_form_with_id_22($form_data)
{
if ( is_user_logged_in() ) {
if ($form_data['id']=='22')
{
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
wp_update_user ( array ( $user_id, 'role' => 'subscriber' ));
}
} else { }
}

I have also tried the first code example you provide, with no success:
e.g.
add_action('cred_before_save_data', 'my_before_save_data_action',10,1);
function my_before_save_data_action($form_data)
{
// if a specific form
if ($form_data['id']==22)
{
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
wp_update_user ( array ( $user_id, 'role' => 'subscriber' ));
}
}

Is there any documentation that you are following?
https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data
https://codex.wordpress.org/Function_Reference/wp_update_user

#1350139

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Andrei,

Thank you for getting in touch.

Actually the correct way to do this is to use the add_role function in wordpress.

Take a look at this link here.
hidden link

Adopting it to your code it would look something like this.


add_action('cred_before_save_data_22', 'before_save_data_for_form_with_id_22',10,1);
function before_save_data_for_form_with_id_22($form_data)
{
if ( is_user_logged_in() ) {
if ($form_data['id']=='22')
{
$current_user = wp_get_current_user();
 $current_user->set_role('subscriber');
}
} else { }
}

Please try this and let me know if it helps.
Thanks,
Shane

#1350185

Works like a charm. My issue is resolved now. Thank you very much! Best wishes!!