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