Navigation überspringen

[Gelöst] Multiple form ID’s in the same cred_save_data function

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

Problem: I have a CRED hook that is used only for specific form IDs. I would like to add some more form IDs to the list, but I'm not sure how the code should be written.

Solution: Modify the code so that the form IDs are managed in a comma-separated list, and check to see if the current form ID is in that list:

function tssupp_false_body( $post_id, $form_data ){
 
$forms = array( 17097,170998 );
 
if ( in_array($form_data['id'], $forms) ){ 
 
$false_body = $_POST['Description'];
 
if ( $false_body ) {
 
$args = array(
'ID' => $post_id,
'post_content' => $false_body
);
 
wp_update_post( $args );
}
}
}
add_action( 'cred_save_data', 'tssupp_false_body', 10, 2 );

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
http://php.net/manual/en/function.in-array.php

This support ticket is created vor 6 Jahren, 8 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 6 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von leilaG vor 6 Jahren, 8 Monaten.

Assistiert von: Christian Cox.

Author
Artikel
#779300

We have used the below code for 2 form ID's but would like to add another 2 form ID's, how would we do that?

function tssupp_false_body( $post_id, $form_data ){

if ( ( $form_data['id'] == 17097 ) OR ($form_data['id'] == 170998) ){ // This applies to either or form ID.

$false_body = $_POST['Description'];

if ( $false_body ) {

$args = array(
'ID' => $post_id,
'post_content' => $false_body
);

wp_update_post( $args );
}
}
}
add_action( 'cred_save_data', 'tssupp_false_body', 10, 2 );

#779978

Try this refactored version, which will make form ID management simpler:

function tssupp_false_body( $post_id, $form_data ){

$forms = array( 17097,170998 );

if ( in_array($form_data['id'], $forms) ){ 

$false_body = $_POST['Description'];

if ( $false_body ) {

$args = array(
'ID' => $post_id,
'post_content' => $false_body
);

wp_update_post( $args );
}
}
}

Add any additional form IDs in the $forms array as a comma-separated list, like this:

$forms = array( 17097,170998,123456,78901 );
#780818

I tried the code and testing the forms, but it doesn't seem to be working, the body field is empty.

#781030

Okay please try these troubleshooting steps first.
- Check the form IDs. The two form IDs in the code you provided seem to be very similar, but slightly off:

$forms = array( 17097,170998 );

Can you confirm that the form IDs are exact matches? I think it is unlikely that these numbers are so similar but have a different number of digits.
- Check the form and $_POST variables for errors. In this code, the field name is "Description". That's a bit unusual, because Type fields are typically lower-case. If it's a Types custom field, this may not be correct. The slug is probably lowercase. If it's a generic field then it could be correct because the field name is arbitrary.
- Test by hard-coding a value for the new body like this:

$false_body = 'This is a test of the cred hook';

Let me know what you find out.

#781415

Thanks for the trouble shooting...

Yes Description is a generic field, here is the code -

<div class="form-group">
<label>Description</label>
[cred_generic_field field='Description' required="true" type='textarea' class='form-control' urlparam='']
{
"required":1,
"validate_format":0,
"default":""
}
[/cred_generic_field]
</div>

Yes the Form ID's are correct, I copy and pasted the below ID's, here is the code I used -

function tssupp_false_body( $post_id, $form_data ){

$forms = array( 17097,17313,17296 );

if ( in_array($form_data['id'], $forms) ){

$false_body = 'This is a test of the cred hook';

if ( $false_body ) {

$args = array(
'ID' => $post_id,
'post_content' => $false_body
);

wp_update_post( $args );
}
}
}

and testing by hard coding.
Still not working.

#785603

Okay please turn on server logging and add error_log statements so we can see more information about what's happening. If you're not familiar with server logging, I can show you how to activate it. Go in your wp-config.php file and look for define(‘WP_DEBUG’, false); Change it to:

define('WP_DEBUG', true);

Then add these lines, just before it says 'stop editing here':

ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');

Add error_log statements to your CRED callback, like this:

function tssupp_false_body( $post_id, $form_data ){
error_log('tssupp_false_body called');
$forms = array( 17097,17313,17296 );

if ( in_array($form_data['id'], $forms) ){
error_log('in array');
$false_body = 'This is a test of the cred hook';

if ( $false_body ) {

$args = array(
'ID' => $post_id,
'post_content' => $false_body
);
error_log(print_r($args, true));
wp_update_post( $args );
}
}
}
add_action( 'cred_save_data', 'tssupp_false_body', 10, 2 );

Finally submit the CRED form again. This should create a file in the root directory of your website called error_log.txt. Please send me its contents.

#804627

Success!!

On the last code you send, I realised there was an extra line that was missing from the code before.

add_action( 'cred_save_data', 'tssupp_false_body', 10, 2 );

Working really well now.

Thanks for your support.