[Resolved] Updating user role when submitting a form
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 role when a post form has been submitted.
Solution:
This can be done by using the following hook.
add_action('cred_save_data', 'change_user_role_on_save',10,2);
function change_user_role_on_save($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==573)
{
$user = wp_get_current_user();
if ( in_array( 'subscriber', (array) $user->roles ) ) {
// Remove role
$user->remove_role( 'subscriber' );
// Add role
$user->set_role('editor');
}
}
}
What you need to do is replace 'subscriber' with the user's current role and then 'editor' with the role that you want the user to gain. Finally just change the 573 to the ID of the post form.
This code can be added to the Toolset custom code snippets at Toolset -> Settings -> Custom Code.
Once it has been added please ensure that you've activated it.
This support ticket is created 3 years, 6 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.
Is there a way to create a form that when submitted, the user role will be changed to something else? For instance, we want to setup up our users to initially be 'Registered' when they create an account. Then, once they submit a directory post, then we want the submission to trigger a role change to 'Active'. Is this possible? I do not see anywhere on the form creation to input this.
Thank you for getting in touch. You should be able to achieve this using the Hook below.
What this does is to remove the registered role from the user and assigns them a new role of active. You will need to add the code below to your custom code section in Toolset -> Settings -> Custom Code and activate it. However you will need to change the 12 to the ID of the form that this should target.
add_action('cred_save_data', 'change_user_role_on_save',10,2);
function change_user_role_on_save($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==12)
{
$user = wp_get_current_user();
if ( in_array( 'registered', (array) $user->roles ) ) {
// Remove role
$user->remove_role( 'registered' );
// Add role
$user->add_role( 'active' );
}
}
}
Thank you for this, my form ID is 300 and I replaced 'registered' with 'Contributor' in both instances and replaced 'active' with Active LC' and then added to custom code and activated. I created a user with role of 'Contributor', submitted form 300 and the role is not changing. Any ideas of what might be missing?
I've made a change to the code and this should get it working.
add_action('cred_save_data', 'change_user_role_on_save',10,2);
function change_user_role_on_save($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==573)
{
$user = wp_get_current_user();
if ( in_array( 'subscriber', (array) $user->roles ) ) {
// Remove role
$user->remove_role( 'subscriber' );
// Add role
$user->set_role('editor');
}
}
}
As per usual replace the 573 with the ID of the form as well as replace subscriber and editor with the respective roles that you want to switch from.
Also once you've added the code to the custom code section, ensure that you've activated it.
I updated the custom code and acitvated it. Still not getting the role to change,
add_action('cred_save_data', 'change_user_role_on_save',10,2);
function change_user_role_on_save($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==300)
{
$user = wp_get_current_user();
if ( in_array( 'Contributor', (array) $user->roles ) ) {
// Remove role
$user->remove_role( 'Contributor' );