Navigation überspringen

[Gelöst] Using the same function for numerous forms

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

Problem:
How can I use the same function for many Toolset Forms (hooked to the Toolset Forms API)

Solution:
https://toolset.com/forums/topic/using-the-same-function-for-numerous-forms/#post-756696

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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

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

Zuletzt aktualisiert von leilaG vor 6 Jahren, 8 Monaten.

Assistiert von: Beda.

Author
Artikel
#753010

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 );

#756696

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():
versteckter 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.

#756745

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

#756971

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 );
#757825

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.

#774316

Is it possible to add more than one 'OR' in the code for multiple forms?

#779214

I have opened another ticket for multiple forms.