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 🙂