Skip Navigation

[Resolved] How to reduce a different users custom user field value by 1 after submitting

This support ticket is created 2 years, 10 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 3 replies, has 2 voices.

Last updated by Christian Cox 2 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#2095871

Tell us what you are trying to do?

This is in close relation to my other ticket which is on this thread = https://toolset.com/forums/topic/how-to-check-for-a-custom-user-field-value-when-inputting-a-cpt-post-form/

So rather than write that all out again (or add it to that ticket), I have split this off into a separate issue to make it more understandable. So, basically, after submitting my 'Reference' custom post type Post Form, I would like for my Custom User Field (slug 'orc-credits') to be reduced down by an increment of 1. The User that this related to is indicated by a relationship field in my Post Form (the slug of which is 'client-reference').

I have tried creating my code in my functions.php - which looking at other bits of code I have been supplied with (which do work), in my head this should work too. But however (of course haha!) it does not work. Please see my custom code below:

add_action('cred_save_data', 'ts_ref_input_decrement_credits_staff_b',10,2);

function ts_ref_input_decrement_credits_staff_b($post_id, $form_data) {

// edit these values as needed
$user_field_slug = 'orc-credits';
$reference_slug = 'client-reference';

$forms = array( 1696 );

// do not edit below this line
if ( in_array( $form_data['id'], $forms ) )
{
$ref_id = isset( $_POST['@' . $reference_slug .'_parent']) ? $_POST['@' . $reference_slug .'_parent'] : 0;
// get the reference post title (email address) and use that to query Users
$ref_title = get_the_title($ref_id);
error_log('ref title: ' . $ref_title);

$user = get_user_by('email', $ref_title);
error_log('user: ' . $user);

// get the User credits information from User field
$user_field = get_user_meta($user->ID, 'wpcf-'. $user_field_slug, true);
error_log('user_field: ' . $user_field);

$credits = get_user_meta( $user->ID, 'wpcf-' . $user_field_slug, true );
error_log('credits: ' . $credits);
if( $credits >= 1 )
{
$credits = $credits-1;
update_user_meta( $user->ID, 'wpcf-' . $user_field_slug, $credits );
}
}

}

So, when I submit my form, and it processes successfully (and publishes a new 'Reference' post), I am then expecting the code above to cross reference the user, then reduce their 'ORC Credits' field by a value of 1. However, what actually happens, is that the form submits (and successfully creates the post), HOWEVER I then end up with a white screen in the wordpress content area (so the header and footer loads but not the content on my success page).

As you will see in my code, I have some error logging setup to debug my code. After running this, when I check my error log, I am getting the following messages:

[22-Jun-2021 15:01:27 UTC] ref title: test10@akcreation.co.uk
[22-Jun-2021 15:01:27 UTC] PHP Fatal error: Uncaught Error: Object of class WP_User could not be converted to string in /var/web/site/public_html/wp-content/themes/yootheme-orc/functions.php:496
Stack trace:
#0 /var/web/site/public_html/wp-includes/class-wp-hook.php(292): ts_ref_input_decrement_credits_staff_b(1737, Array)
#1 /var/web/site/public_html/wp-includes/class-wp-hook.php(316): WP_Hook->apply_filters(NULL, Array)
#2 /var/web/site/public_html/wp-includes/plugin.php(484): WP_Hook->do_action(Array)
#3 /var/web/site/public_html/wp-content/plugins/cred-frontend-editor/application/models/form/post.php(515): do_action('cred_save_data', 1737, Array)
#4 /var/web/site/public_html/wp-content/plugins/cred-frontend-editor/application/models/form/base.php(450): CRED_Form_Post->save_form(1737)
#5 /var/web/site/public_html/wp-content/plugins/cred-frontend-editor/application/controllers/form_builder_base.php(42): CRED_Form_Base->print_form()
#6 /var/web/site/public_html/wp-content/plugins/cred-frontend-editor/library/toolset/cred/embedded/classes/CRED_Helper.p in /var/web/site/public_html/wp-content/themes/yootheme-orc/functions.php on line 496

Line 495 and 496 in my functions.php file relate to these lines in my code above:

$user = get_user_by('email', $ref_title);
error_log('user: ' . $user);

So, I then go into WordPress to check the new 'Reference' post to see if there is anything which may offer a clue, and there is:

- Basically (just to add to the confusion, sorry), when a 'Reference' post is posted, instead of the 'post author' being the user which inputs the Post Form, I have a relationship field (the 'client-reference' field) which allows the selection of a 'Client' email address, and I then have the following code in my functions.php file which runs, and changes the 'post author' of the 'Reference' to match the email address in that field. This is the code which does that:

// Update the Reference 'Post' (ie Reference CPT) from Post Form (for Staff Input ID 1696)
// input with Client email as author

add_action('cred_submit_complete', 'my_update_data_activity_1696',10,2);
function my_update_data_activity_1696($post_id, $form_data)
{

if ($form_data['id']==1696) {

$km_post_id = toolset_get_related_post($post_id, 'client-reference', 'parent');
$km_post_author_id = get_post_field('post_author', $km_post_id);

// Create post object
$my_post = array(
'ID' => $post_id,
'post_author' => $km_post_author_id
);

// Update the post into the database and save new Activity post ID
wp_update_post( $my_post );

}
}

Now - THAT code above was working perfectly (ie the post author was being changed to the client email address being selected, lets say for example my client email 'test10@akcreation.co.uk'). HOWEVER, since adding the function at the start of this thread - 'function ts_ref_input_decrement_credits_staff_b($post_id, $form_data)', this has now STOPPED working, and the author shows as 'Keith Mason' (which is me), instead of 'test10@akcreation.co.uk'.

So - to summarise, I'm guessing that I am somehow using my scripts in the wrong order? ie the 'cred_save_data' script needs to be after the 'cred_submit_complete' script in order to allow for the post_author to be updated before I can cross reference the 'ORC Credits' field??

Do I possibly just need to put the 2 bits of custom code together in the 'cred_submit_complete' hook? Making sure they are ordered correctly?

I'm sorry for the long and confusing thread, but what I thought would be so simple to achieve isn't, and hopefully I have explained it all in a way which makes sense?

Thank you so much for any assistance you can provide, I can assure you this is the last issue on this project that needs fixing and then I am going away on holiday to de-stress for a week haha!

Cheers,

Keith

Is there any documentation that you are following?

I created this code using a mixture of the following 2 threads:
1) https://toolset.com/forums/topic/allow-users-to-input-a-set-number-of-posts-via-user-form/
2) https://toolset.com/forums/topic/how-to-check-for-a-custom-user-field-value-when-inputting-a-cpt-post-form/ (this is the thread mentioned earlier)

Is there a similar example that we can see?

no

What is the link to your site?

I can provide links / logins etc if required, thanks 🙂

#2096159

Hello again! This error is blocking the execution of the rest of the function, which would decrement the custom field value:

[22-Jun-2021 15:01:27 UTC] PHP Fatal error: Uncaught Error: Object of class WP_User could not be converted to string in /path/to/site/functions.php:496


Line 495 and 496 in my functions.php file relate to these lines in my code above:

$user = get_user_by('email', $ref_title);
error_log('user: ' . $user);

Okay the error is happening because the information you are passing into error_log is too complex to be automatically converted into a simple text string. The value of $user is a WP_User object. That is good, because it means the query found a User. The error_log function then attempts to convert that value to a simple string but it can't handle that conversion. You can work around that by using print_r in the error_log call to debug a variable storing a more complex value like an object or indexed array:

$user = get_user_by('email', $ref_title);
if( $user ) { // get_user_by returns a WP_User object or false
  error_log('user');
  error_log( print_r($user, true)); // debug the WP_User object
}

Give that a shot, let me know the results.

#2096789

Ha - I love it - so the error wasn't my code, the error was my error checking haha!

Once I fixed that by swapping with your error check code, it works! The ORC Credits field is now reducing by an increment of 1 after I submit the post. So at least the rest of my code was correct this time 🙂 And now I know how to check the WP_User Object correctly - so all part of the learning process.

Thank you so much Christian! I will close this ticket

Keith

#2097235

Great! Glad to help, that's what we are here for. Feel free to reach out if you have additional concerns.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.