I have a membership site. I have few site managers each providing me with single tasks. I want to avoid allowing these managers access to the backend.
This is my need
I have many users under the role "subscriber", and from time to time there is a large group to move from subscriber to author.
is it possible to promote the user from "subscriber" role to "author" role in the frontend using access form, without the need for my managers to login into the my dashboard backend ?
Thank you for contacting us and I'd be happy to assist.
As there is no built-in option available in the user forms to change the user's role from the front-end, this is possible using some custom code.
You can create a user edit form, which only has a submit button without any actual fields. When the form is submitted, you can attach a custom function through the "cred_save_data" hook, which removes the "subscriber" role and assigns the "author" role to the user who is being edited with the form.
( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data )
I created a form to edit user with the intent to promote from "subscriber" to "author" when edit form is submitted.
Saved edited form into an unassigned content template
I adjusted the access settings in the forms tab and allowing my manager with credentials to edit/promote users.
I created the following snippet code in toolset>settings>custom code area and activated it.
I copied the form ID and added it to the snippet code.
add_action('cred_save_data', 'func_update_user_role_action',10,2);
function func_update_user_role_action($user_id, $form_data) {
if ($form_data['id'] == 870) {
// modify the user role to match the selected option
$u = new WP_User( $user_id );
$u->remove_role( 'subscriber' ); /// adjust role name here if needed
$u->add_role( 'author' );
}
}
When testing the form as manger with proper credentials to edit, I got an error message "The user was not saved because of the following problem:
This form can not edit users with a role of subscriber "
Also, when I tried to use the form as administrator, it copied the current user login ID and displayed in the form without the option to change it. If I submitted it my credentials as admin would have switched to author as well. Somehow the code needed to have a function not allowing to self promote for that matter.
I don't know what else I should be doing, or if I missed a step using the hook function code.
Thank you for waiting as this investigation took longer than I initially expected.
During testing on my website with similar settings and custom code, the form worked.
After further debugging on your website, I was able to make the edit form work, by making these 2 changes:
1. In the "Promote User to Author - User Form" settings, I selected the "Subscriber" role, instead of "Author", since, before the form's submission, the user's role is "Subscriber".
( screenshot: hidden link )
This fixed the message about the incorrect user role being edited by the form.
2. The next challenge was that the custom function to change the user role was not executing with the form submission. I noticed that in the custom code snippet's settings, the "Run mode" was set to "On demand" and not "Run Always".
( screenshot: hidden link )
Changing the run mode to always fixed the code's execution and I apologize, I should've caught this sooner.
For me it did not take much time as I waited. It is paid off with the results. I am sure this will also help others since this feature adds value to the toolset functionalities as a whole. The idea of being able to perform in the front end is what differentiates TOOLSET from all other plugins.