This thread is resolved. Here is a description of the problem and solution.
Problem: I would like to use a cred_save_data hook to update a User's role when a Form is submitted.
Solution: Use the following custom code to set a User's role to be "seller":
add_action('cred_save_data', 'cred_update_user_role_action',10,2);
function cred_change_user_role_action($post_id, $form_data) {
if ($form_data['id'] == 205164) {
// get the author ID and corresponding WP_User from the post ID
$p = get_post( $post_id );
$author_id = $p->post_author;
$u = new WP_User( $author_id );
// modify the user role to match the selected option
$u->remove_role( 'pending' ); /// adjust role name here if needed
$u->add_role( 'seller' );
}
}
I'm trying to create a three step process that requires a new user role for each step
1. User creates account via cred user form
creates user with 'Applicant' role
This is done and working
2. User submits cred post form
creates pending post. changes author of post from "applicant" to "pending" role
I am using the following without success
[php]
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
3. User in "staff" role reviews pending post on front end, clicks button to approve post and changes author user role from "pending" to "approved"
I haven't gotten to testing this yet but I thought I'd include in the event something is obviously wrong, which is likely the case.
[php]
add_action('cred_save_data', 'cred_update_user_role_action',10,2);
function cred_update_user_role_action($user_id, $form_data) {
if ($form_data['id'] == 205157) {
// modify the user role to match the selected option
$u = new WP_User( $user_id );
$u->remove_role( 'pending' ); /// adjust role name here if needed
$u->add_role( 'approved' );
}
}
[php]
Could I get some help on getting this right please?
3. User in "staff" role reviews pending post on front end, clicks button to approve post and changes author user role from "pending" to "approved"
When the staff member reviews the pending post on the front-end, does that mean the staff member is submitting an "edit post" Form that changes the post status to "Published"? If so, then that Form is not directly connected to the User who submitted the original post for review. That Form is all about editing the post, so the first parameter in the callback will not be the user's ID, it will be the post's ID. You would have to get the original author's ID from that post ID to be able to modify the original User's role.
add_action('cred_save_data', 'cred_update_user_role_action',10,2);
function cred_update_user_role_action($post_id, $form_data) { // notice the first param is not the user ID here because this is an edit post form
The only thing I care about is the person in the "staff" role having a button to push that changes the post author's role. What is your best suggestion to get that done?
My php knowledge is about as close to zero as possible. I've been trying to use inexact examples I've found here in the forums. Any guidance to getting this done would be greatly appreciated.
My suggestion is to use an Edit Post Form that edits the post submitted by the pending User. This will modify the post status to be "published". Then you will use the cred_save_data API to change the User role.
Did the change I proposed for #2 work as expected? Let's get that part of the process working before moving on to #3. If not, I'll need to log in and take a closer look.
Yes sir. When the user submits the form their role changes as intended. Thank you so much.
On the 3rd step, I have implemented the edit post form with no fields and the single button, which works as intended, by changing post status from pending to published. I've also taken note, as you displayed, of calling the post ID first. I've made some attempts at implementing from there, but haven't had luck getting the user role to change.
I've made some attempts at implementing from there, but haven't had luck getting the user role to change.
Okay not sure what you've tried so far, but here's what I was thinking. You will get the edited post information using get_post and the post ID, then use that to get the author ID. Use the author ID to get a WP_User, then the rest of the role update code should remain the same:
add_action('cred_save_data', 'cred_update_user_role_action',10,2);
function cred_update_user_role_action($post_id, $form_data) {
if ($form_data['id'] == 205157) {
// get the author ID and corresponding WP_User from the post ID
$p = get_post( $post_id );
$author_id = $p->post_author;
$u = new WP_User( $author_id );
// modify the user role to match the selected option
$u->remove_role( 'pending' ); /// adjust role name here if needed
$u->add_role( 'approved' );
}
}
add_action('cred_save_data', 'cred_update_user_role_action',10,2);
function cred_change_user_role_action($post_id, $form_data) {
if ($form_data['id'] == 205164) {
// get the author ID and corresponding WP_User from the post ID
$p = get_post( $post_id );
$author_id = $p->post_author;
$u = new WP_User( $author_id );
// modify the user role to match the selected option
$u->remove_role( 'pending' ); /// adjust role name here if needed
$u->add_role( 'seller' );
}
}
I tried the following:
I had to change the function name because the previous the previous action we discussed earlier in the thread has the same function name and threw an error.
I tried the form ID of both the user form and the "staff" form, which is just the single button.
In both cases, the post status changed but not the user status.
To display the button I've included the form, just the button, into a content template and displayed the template onto the post. I thought this would be the only way to prevent the user level from seeing the button be displayed.
Christian is sick today. I see you are in the middle of an issue that he is familiar with, so I will let him continue with this when he returns tomorrow. (If he ends up being away longer then I will take this on.)