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 );
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 );
I tried the code and testing the forms, but it doesn't seem to be working, the body field is empty.
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.
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.
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.
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.