Skip Navigation

[Resolved] many forms ID in a snippet

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)

This topic contains 1 reply, has 2 voices.

Last updated by Waqar 1 year, 11 months ago.

Assisted by: Waqar.

Author
Posts
#2537949

Tell us what you are trying to do?
I need to set the post title from many different forms.
I found this snippet in this topic: https://toolset.com/forums/topic/define-a-custom-post-title-using-information-from-the-form/
But I'm not php expert and I wondering how to use the sam snippet for many forms IDs
This is my guess:

add_action('cred_save_data','custom_title_slug_func',15,2);
function custom_title_slug_func($post_id, $form_data) {
if ($form_data['id']==123 || $form_data['id']==456) {

// get the value of fields
$field_1 = $_POST['wpcf-field-slug-1'];
$field_2 = $_POST['wpcf-field-slug-2'];

// set the values of fields in title and slug
$title = $field_1;
$slug = $field_2;

$my_post_data = array(
'ID' => $post_id,
'post_title' => $title,
'post_name' => sanitize_title($slug),
);

// Update the post into the database
wp_update_post( $my_post_data );

}
}

Am I right?

Thank you

#2538537

Hi,

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

Your approach where the OR ( || ) operator joins multiple conditions can work, but it will become a little too complicated, with the increasing number of target form IDs.

A simpler alternative can be to create a list/array of the target form IDs and then check if the current form is one of them:


add_action('cred_save_data','custom_title_slug_func',15,2);
function custom_title_slug_func($post_id, $form_data) {
	// list of target form IDs
	$form_array = array(123, 456, 789);
	// if the current form is one of the target forms
	if (in_array($form_data['id'], $form_array)) {
		// get the value of fields
		$field_1 = $_POST['wpcf-field-slug-1'];
		$field_2 = $_POST['wpcf-field-slug-2'];

		// set the values of fields in title and slug
		$title = $field_1;
		$slug = $field_2;

		$my_post_data = array(
			'ID' => $post_id,
			'post_title' => $title,
			'post_name' => sanitize_title($slug),
		);

		// Update the post into the database
		wp_update_post( $my_post_data );

	}
}

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

regards,
Waqar