Navigation überspringen

[Gelöst] Dynamic Field values from CPT

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem: I want to create a select field with multiple options, and set the value of another custom field depending on the value selected in the select field.

Solution: You can use custom code with the cred_save_data API to get the selected value from one field and save a corresponding value into a Types custom field programmatically. You can find the field selections in the $_POST superglobal. Types fields have a key of "wpcf-" plus the field slug. Generic fields have a key identical to the field slug. So if the select field is a Types field with the slug "select-field", then you would be able to get the selected value like so:

$_POST['wpcf-select-field']

If it is a generic field with the slug "select-field", then you would be able to get the selected value like so:

$_POST['select-field']

Use the WP function update_post_meta to set a Types field value programmatically. A full example:

add_action('cred_save_data', 'tssupp_set_selected_value',10,2);
function tssupp_set_selected_value($post_id, $form_data) {
  $forms = array( 123 );
  if ( in_array( $form_data['id'], $forms ) )
  {
    $selected = $_POST['wpcf-select-field'];
    update_post_meta( $post_id, 'wpcf-points-field', $selected);
  }
}

You would change 123 to be the numeric ID of the Form, wpcf-select-field to be the slug of the select field (with the wpcf- prefix if this is a Types field), and wpcf-points-field to be the slug of the points field (with the wpcf- prefix if this is a Types field). That would save the selected value of the select field into the points field when the Form is submitted.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://developer.wordpress.org/reference/functions/update_post_meta/

This support ticket is created vor 4 Jahren, 4 Monaten. 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)

Dieses Thema enthält 4 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von larryL vor 4 Jahren, 4 Monaten.

Assistiert von: Christian Cox.

Author
Artikel
#1735649

Continuing from where I left on https://toolset.com/forums/topic/pass-value-to-number-field-in-cred/

I decided to create a select field with multiple options, depending upon which options the user selects a different number field appears (condtionally) with a set value in it.

IOW - Option A could be 1, Option B could be .5, Option C could be 3, etc...

What I am wondering is how I can save that selected value to a 'points' number field. So if the user selects 'option B' which has the value of .5 - .5 is also displayed AND saved to the 'points' field.

Thanks!

Larry

#1736331

I decided to create a select field with multiple options, depending upon which options the user selects a different number field appears (condtionally) with a set value in it.
You can use custom code with the cred_save_data API to get the selected value from one field and save a corresponding value into a Types custom field programmatically. You can find the field selections in the $_POST superglobal. Types fields have a key of "wpcf-" plus the field slug. Generic fields have a key identical to the field slug. So if the select field is a Types field with the slug "select-field", then you would be able to get the selected value like so:

$_POST['wpcf-select-field']

If it is a generic field with the slug "select-field", then you would be able to get the selected value like so:

$_POST['select-field']

Use the WP function update_post_meta to set a Types field value programmatically. A full example:

add_action('cred_save_data', 'tssupp_set_selected_value',10,2);
function tssupp_set_selected_value($post_id, $form_data) {
  $forms = array( 123 );
  if ( in_array( $form_data['id'], $forms ) )
  {
    $selected = $_POST['wpcf-select-field'];
    update_post_meta( $post_id, 'wpcf-points-field', $selected);
  }
}

You would change 123 to be the numeric ID of the Form, wpcf-select-field to be the slug of the select field (with the wpcf- prefix if this is a Types field), and wpcf-points-field to be the slug of the points field (with the wpcf- prefix if this is a Types field). That would save the selected value of the select field into the points field when the Form is submitted.
API documentation is available here:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://developer.wordpress.org/reference/functions/update_post_meta/

#1741697

Here is the section of the form I'm dealing with:



	<div class="row">
		<div class="form-group col-sm-12">
			<label>Leave Type Points</label>
			[cred_field field='leave-type-points' force_type='field' class='form-control' output='bootstrap']
		</div>
	</div>
	<div class="row">
		<div class="col-sm-1">
	[cred_show_group if="( $(leave-type-points) eq 'Sick Leave' ) OR ( $(leave-type-points) eq 'Vacation' ) OR ( $(leave-type-points) eq 'Bereavement' )" mode="fade-slide"]
	<div class="row">
		<div class="form-group col-sm-12">
			<label>Excused</label>
			[cred_field field='leave-type-zero' force_type='field' class='form-control' output='bootstrap']
		</div>
	</div>[/cred_show_group]
		</div>
		<div class="col-sm-1">
	[cred_show_group if="( $(leave-type-points) eq 'Late Call Off' )" mode="fade-slide"]
	<div class="row">
		<div class="form-group col-sm-12">
			<label>Late Call Off</label>
			[cred_field field='late-call-off' force_type='field' class='form-control' output='bootstrap']
		</div>
	</div>[/cred_show_group]
		</div>
		<div class="col-sm-1">
	[cred_show_group if="( $(leave-type-points) eq 'Left Post Early' )" mode="fade-slide"]
	<div class="row">
		<div class="form-group col-sm-12">
			<label>Left Post Early</label>
			[cred_field field='left-post-early' force_type='field' class='form-control' output='bootstrap']
		</div>
	</div>[/cred_show_group]
		</div>
		<div class="col-sm-1">
	[cred_show_group if="( $(leave-type-points) eq 'No Call / No Show' )" mode="fade-slide"]
	<div class="row">
		<div class="form-group col-sm-12">
			<label>No Call / No Show</label>
			[cred_field field='no-call-no-show' force_type='field' class='form-control' output='bootstrap']
		</div>
	</div>[/cred_show_group]
		</div>
		<div class="col-sm-2">
	[cred_show_group if="( $(leave-type-points) eq 'Other' )" mode="fade-slide"]
	<div class="row">
		<div class="form-group col-sm-12">
			<label>Enter other points</label>
			[cred_field field='other-ltp' force_type='field' class='form-control' output='bootstrap']
		</div>
	</div>[/cred_show_group]
		</div>
		<div class="col-sm-2">
	[cred_show_group if="( $(leave-type-points) eq 'Tardy (+/- 30min)' )" mode="fade-slide"]
	<div class="row">
		<div class="form-group col-sm-12">
			<label>Tardy (+/- 30min)</label>
			[cred_field field='tardy-plus-minus-30' force_type='field' class='form-control' output='bootstrap']
		</div>
	</div>[/cred_show_group]
		</div>
		<div class="col-sm-2">
	[cred_show_group if="( $(leave-type-points) eq 'Tardy (30 min+)' )" mode="fade-slide"]
	<div class="row">
		<div class="form-group col-sm-12">
			<label>Tardy (30 min+)</label>
			[cred_field field='tardy-plus-30' force_type='field' class='form-control' output='bootstrap']
		</div>
	</div>[/cred_show_group]
		</div>
		<div class="col-sm-2">
	[cred_show_group if="( $(leave-type-points) eq 'Tardy (61 min+ no notice)' )" mode="fade-slide"]
	<div class="row">
		<div class="form-group col-sm-12">
			<label>Tardy (61 min+ no notice)</label>
			[cred_field field='tardy-61' force_type='field' class='form-control' output='bootstrap']
		</div>
	</div>[/cred_show_group]
		</div>
	</div>
	

Can I still use the code you posted? Is this doable?

Thanks

#1742451

It seems fine to me. I don't see any reason you shouldn't be able to use the cred_save_data hook to capture the information stored in one or more of the fields and save that information in another field or fields when this form is submitted. I don't see any generic fields used here, so you would use the wpcf- prefix with the field slug to access the saved field information in the $_POST superglobal, and then you could save that information somewhere else if you'd like using update_post_meta. If you're having trouble getting this to work I would need to see the code you have implemented, and I would need to know where else you want to save the field information when the Form is submitted.

#1758675

My issue is resolved now. Thank you!