Navigation überspringen

[Gelöst] CRED: assign value of another field, if field is empty

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

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

Zuletzt aktualisiert von Lara vor 4 Jahren, 5 Monaten.

Assistiert von: Waqar.

Author
Artikel
#1707335

Tell us what you are trying to do?

I have to customs fields in a CRED form. Field B ('wpcf-rask-bis-tag') should have the same value like field A ('wpcf-rask-von-tag'), if the field is empty, when the CRED form is submitted

add_action('cred_before_save_data', 'func_assign_field_value',10,1);
function func_assign_field_value($form_data)
{
  
    // if a specific form
    $forms = array( 5629 , 10440 , 10362 );
    if (in_array($form_data['id'], $forms)) 


    {
        if (isset($_POST['wpcf-rask-bis-tag'])) {
          $_POST['wpcf-rask-bis-tag'] = $_POST['wpcf-rask-von-tag'];  
        }
    }
}

What is the problem?
Now Field B has always the value of Field A - no matter if it was empty previously or not ..

Is there any documentation that you are following?
Yes, there was a documentation, but I can't find it anymore.

Is there a similar example that we can see?
In the documenatation, that I lost

What is the link to your site?
https:// cuteberry.de

#1707487

Hi,

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

If your goal is to make "wpcf-rask-bis-tag" field use the same value as the "wpcf-rask-von-tag" field, only when "wpcf-rask-bis-tag" field's own value is empty, you can adjust your code to:


add_action('cred_before_save_data', 'func_assign_field_value',10,1);
function func_assign_field_value($form_data)
{
	// if a specific form
	$forms = array( 5629 , 10440 , 10362 );

	if (in_array($form_data['id'], $forms)) 
	{
		// if value of wpcf-rask-bis-tag field is empty
		if (empty($_POST['wpcf-rask-bis-tag'])) {
			// make wpcf-rask-bis-tag field use the same value as the wpcf-rask-von-tag field
			$_POST['wpcf-rask-bis-tag'] = $_POST['wpcf-rask-von-tag'];  
		}
	}
}

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1707623

Thanks Waqar! Now it works perfectly.
Using empty instead of isset solved the problem 🙂

#1709095

I try to re-open. I didn't test properly. Today a beta-tester pointed out that it still doesn't work.
The wpcf-rask-bis-tag is a datepicker field. Is it possible, that it is not empty by default? I mean, even before the user set a date?