Skip Navigation

[Resolved] Cred Hooks Issues

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' );
  }
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 5 years, 9 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 9 replies, has 3 voices.

Last updated by mikeF-8 5 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#1209967

Hello,

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) {

if ($form_data['id']=='205164')
{
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
wp_update_user ( array ( $user_id, 'role' => 'pending' ));
}
}
[php]

Where am I going wrong here?

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?

#1210181

2. User submits cred post form
Check your User data array syntax. The "ID" key is missing, so it should probably be changed to this:

wp_update_user ( array ( 'ID' => $user_id, 'role' => 'pending' ) );

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
#1210296

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.

Thanks so much for your help.

#1210573

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.

#1212016

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.

Your help would be appreciated.

#1212652

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' );
  }
}

Try that and let me know the results.

#1212753

Hello Sir,

I added the following to my functions.php file:

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.

I think that's all the variables in play here.

Thanks again for your continued help.

#1213219

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

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.)

#1213712

It looks like you changed the function name, but not the action assignment:

add_action('cred_save_data', 'cred_update_user_role_action',10,2);
function cred_change_user_role_action($post_id, $form_data) {

Those function names should match:

add_action('cred_save_data', 'cred_change_user_role_action',10,2);
function cred_change_user_role_action($post_id, $form_data) {
#1214021

Thank you for your help and patience, Christian. It works as intended. I hope you are feeling well again.