Skip Navigation

[Resolved] Editing button in cred form

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/Karachi (GMT+05:00)

This topic contains 1 reply, has 2 voices.

Last updated by Waqar 1 year, 2 months ago.

Assisted by: Waqar.

Author
Posts
#2671413
website.jpg
cred-form.jpg

I am using cred form to create button like that will allow users to like the posts.
I am having trouble editing my cred form, i have custom field 'like', i want to set its value to be 0, then i want to edit my button 'heartButton' to have click function that will add +1 value to my like field.
I provided picture of website, instead of input field and submit button it should only be button and when clicked it should add +1 value to my field.

#2671591

Hi,

Thank you for contacting us and I'd be happy to assist.

To achieve something like this, you can add a 'number' type custom field and then make an edit form, with only the submit button.
( you don't need any field in this form )

To increment the value of this field, with each form submission, you can use a custom function attached to the 'cred_save_data' hook:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

For example, if the form's ID is '12345' and the target custom field slug for the likes is 'likes-field-slug', the code will look like this:


add_action('cred_save_data','custom_like_add_func',15,2);
function custom_like_add_func($post_id, $form_data) {
	// check if a specific form
	if ($form_data['id']==12345) {
		// slug of the target custom field
		$like_field_slug = 'likes-field-slug';
		// get current likes value
		$likes_value_current = types_render_field( $like_field_slug, array( "output" => "raw", "item" => $post_id ));
		// if no value set it to 0
		if(empty($likes_value_current)) {
			$likes_value_current = 0;
		}
		// add 1 to the existing value
		$likes_value_new = $likes_value_current + 1;
		// update the new value to the custom field
		update_post_meta( $post_id, 'wpcf-'.$like_field_slug, $likes_value_new );
	}
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

regards,
Waqar

#2671629

Thank you Waqar for your help, this is working without any problems!