Skip Navigation

[Resolved] Unable to get numeric custom field to decrease in increments of 1…

This support ticket is created 2 years, 11 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 2 voices.

Last updated by KeithM7209 2 years, 11 months ago.

Assisted by: Luo Yang.

Author
Posts
#2011283

Tell us what you are trying to do? Please see this previous support thred = https://toolset.com/forums/topic/allow-users-to-input-a-set-number-of-posts-via-user-form/
Where Christian very kindly gave me a code snippet (please see his first response, and the text "4. The ability to automatically subtract one "credit" each time the User posts a new post with Forms", this is what I am referring to here. The code snippet that I was supplied for that function, I have tried to add to the functions.php file in my child theme, but it is not working (ie after I input a post form for my custom post type, it is not reducing my User Custom Field 'ORC Credits' by an increment of 1, the field value is staying the same as before.

Is there any documentation that you are following?

I am following the code snippet I was supplied in that thread, however I have updated it to work with my Custom Field / Post Form, this is the code I am using in my functions.php file in my child theme:

// automatically subtract 1 from the current user's credits field
// when a new post form is submitted
// https://toolset.com/forums/topic/allow-users-to-input-a-set-number-of-posts-via-user-form/
add_action('cred_save_data', 'tssupp_decrement_credits',10,2);
function tssupp_decrement_credits($post_id, $form_data) {

// edit these values as needed
$slug = 'orc-credits';
$forms = array( 146 );

// do not edit below this line
if ( in_array( $form_data['id'], $forms ) )
{
$user = wp_get_current_user();
$credits = get_user_meta( $user->ID, 'wpcf-' . $slug, true );
if( $credits >= 1 )
{
$credits = $credits-1;
update_user_meta( $user->ID, 'wpcf-' . $slug, $credits );
}
}
}

** orc-credits is the slug for my User Custom Field (ORC Credits), and the form ID of my Post Form for my custom post type (References) is 146 (which is what I have put in the $forms variable.

Is there a similar example that we can see?
I can give you access to the form page if required, but it is not publically accessible (it is only viewable for certain User Roles), so if you want access I can set you up with a login no problem.

What is the link to your site?

hidden link (I would like this link hidden from public view if possible please)

If you need any more information from me please do not hesitate to ask.

Thanks very much for your help,

Keith

#2011513

Hello,

It is a custom PHP codes problem, please provide a test site with the same problem, fill below private message box with test site credentials, also point out the problem page URL and form URL, where I can edit your custom PHP codes, I need a live website to test and debug it, thanks

#2012921

Thanks for the details, I can login into your website, will update here if find anything

#2013093

I assume we are talking about this post form:
hidden link
Above post form's ID is 57

So I have done below modifications in your website:
Edit your theme file "functions.php", change line 139 from:

$forms = array( 146 );

To:

$forms = array( 57, 146 );

Please test again, check if it is fixed, thanks

#2016989

Thank you very much for your help Luo - and my apologies - I guess I must've been tired when I added that code snippet and used an incorrect form ID!

I will mark this as resolved now - thanks again for your help 🙂

#2016995

My issue is resolved now. Thank you!

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