Hello,
How do I use the below function for more than one form?
function tssupp_false_body( $post_id, $form_data ){
if ( $form_data['id'] == 17097 ) { // Edit
$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 );
Using PHP "OR" logic:
if ( $form_data['id'] == 17097 ) { // This applies to one ID only
if ( ( $form_data['id'] == 17097 ) OR ($form_data['id'] == 170998){ // This applies to either or form ID.
Of course, this is the most simple way, there are more DRY ways to do the same, for example, putting all ID's into an array and then checking if in_array():
hidden link
However this is subject to knowledge of PHP Code in general, which we do not communicate widely - we support mostly Toolset API questions.
For this task, generic PHP knowledge can be applied.
I think with above examples you should be able to craft the code as you wish.
I added the code, but got the below error -
Parse error: syntax error, unexpected 'OR' (T_LOGICAL_OR) in /nas/content/live/biocitygroup/wp-content/themes/bridge-child/functions.php on line 63
The exact code as you posted, altered with my suggestion, does not throw that error:
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 );
Instead, it throws "Parse error: syntax error, unexpected ';'".
It's because I made a typo in the basic PHP syntax and forgot a closing ")" in my code.
The correct suggested code is:
if ( ( $form_data['id'] == 17097 ) OR ($form_data['id'] == 170998) ) { // This applies to either or form ID.
Hence:
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 );
Great! Thanks that worked!
If I wanted to add another form ID, is that possible? For example... I have 4 forms that needed the same function.
Is it possible to add more than one 'OR' in the code for multiple forms?
I have opened another ticket for multiple forms.