Skip Navigation

[Resolved] Post Form custom function code generation

This thread is resolved. Here is a description of the problem and solution.

Problem:

Update a custom field value after submit the post form.

Solution:

I suggest you try to setup custom PHP codes with action hook cred_save_data.

Relevant Documentation:

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

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

Last updated by collectiveU 5 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#1324765

I have the following post form on the URL hidden link when the "Claim Mini Golf" button is clicked.

[credform]
	[cred_field field="form_messages" class="alert alert-warning"]
	<div class="form-group hidden">
		[cred_field field="post_title" class="form-control" output="bootstrap" value='BY_[wpv-user field="user_email"]_FR_[types usermeta="member-free-rounds" format="FIELD_VALUE"][/types]']
	</div>
	<div class="form-group hidden">
		[cred_field field="rounds-user-id" force_type="field" class="form-control" output="bootstrap" value="[wpv-user field='ID']" readonly]
	</div>
	<div class="form-group hidden">
      [cred_field field='free-round-course-name' force_type='field' class='form-control' output='bootstrap' value='CourseName']
	</div>
	<div class="form-group hidden">
		[cred_field field='rounds-booked-on' force_type='field' class='form-control' output='bootstrap' value='[wpv-post-today]']
	</div>
	<div class="form-group hidden">
		[cred_field field="round-booking-code" force_type="field" class="form-control" output="bootstrap" value='[fg-freeround-code]']
	</div>
	<div class="form-group">
      <p>Thanks for booking your Mini Golf round!</p>
      <p>You can proceed to the venue and claim your benefit by showing them the voucher number.</p>
      <p>You're limited to one free mini golf round per applicable venue, per membership year.</p>
      <p>This voucher is to be used within 30 days</p>
      <p>Enjoy the round!</p>
	</div>
	<div class="form-group hidden">
		[cred_field field="@golf-course-free-round.parent" class="form-control" output="bootstrap" select_text="--- not set ---"]
		[cred_field id="field-related-partner-id" field="related-partner-id" force_type="field"]
        [cred_field field='free-round-type' force_type='field' class='form-control' output='bootstrap' value='2']
[cred_generic_field type='hidden' field='special-course-contact-id']
{
"default":"27",
"generic_type":"user_id",
"persist":1
}
[/cred_generic_field]
	</div>
[cred_field field="form_submit" output="bootstrap" value="Claim Now!" class="btn btn-fg-important"]
[/credform]

The

[fg-freeround-code]

is being called to a random function code generator that I've created.
The following is my function:

add_shortcode('fg-freeround-code', 'fg_random_booking_code');
function fg_random_booking_code() {
  
	$characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
 	$string = '';
 	$max = strlen($characters) - 1;
  
      for ($i = 0; $i < 10; $i++) {
        $string .= $characters[mt_rand(0, $max)];
      }
  
	return $string;
};

My question is... how do I modify the post form so that based on a certain condition, another function is called and the value is assigned to "round-booking-code"?
The function I'm proposing to call is the following which for now finds any posts of post_type "manual-voucher-code" where the wpcf-date-used is null

add_shortcode('fg-freeround-code-manual', 'fg_booking_code_from_manual_voucher_codes');
function fg_booking_code_from_manual_voucher_codes() {
  
	// find manual voucher
	$token_query = get_posts(
		array(
			'post_type' => 'manual-voucher-code',
			'meta_key' => 'wpcf-date-used',
			'meta_value' => array(''),
			'meta_compare' => 'NOT IN'
		)
	);

	// does token exist?
	if (sizeof($token_query) > 0){

		$voucher_code = get_posts( $token_query );
		foreach( $voucher_code as $vcode ) {
			$vcode_id = $vcode->ID;
			$meta = get_post_meta($vcode_id, 'wpcf-voucher-code', true);
			return $meta;
		}
	}	
};
#1324807

Hello,

There isn't such kind of built-in feature within Toolset Forms plugin, I suggest you try these:
1) After user submit the form, use action hook cred_save_data to trigger a PHP function:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

2) In this PHP function do these:
a) Find any posts of post_type "manual-voucher-code" where the wpcf-date-used is null
b) Then update the custom field "round-booking-code" value:
https://codex.wordpress.org/Function_Reference/update_post_meta

#1325211

Hi Luo,
I've added the following & even still, the $post_id for the save isn't having the $wpcf-round-booking-code value updated.
Please advise.
Cheers

add_action('cred_save_data_109163', 'manual_voucher_code_search', 10, 2);
function manual_voucher_code_search($post_id, $form_data) {

	// find tokens
	$token_query = 	(
		array(
			'post_type'      => 'manual-voucher-code',
			'posts_per_page' => 1,
			'post_status' => 'publish',
			'meta_query' => array(
				'relation' => 'AND',
				array(
					'key' => 'wpcf-voucher-type',
					'compare' => '=',
					'value' => '2'
				)
			)
		)
	);
	// 
	if (sizeof($token_query) > 0 ){
		$token_value = get_post_meta($token_query->ID, 'wpcf-voucher-code', true);
		update_post_meta($post_id, 'wpcf-round-booking-code', $token_value);
	}
};
#1325583

My issue is resolved now. Thank you!