Skip Navigation

[Resolved] Auto-title from postform

This support ticket is created 5 years, 3 months ago. 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by samuelH 5 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#1351195

Hi,
As discussed in this tread: https://toolset.com/forums/topic/custom-title-from-post-form/ I am now able to autogenerate a post title when submitting a form.

This is the code used:

***********
function my_save_data_action5($post_id, $form_data){
// Change your CRED Form "ID" accordingly below
if ($form_data['id']==1577){

//Declare the content of your variables, change "your_custom_field_slug" accordingly
$custom_title = 'Rapportnummer ' . $post_id;

//collect data and define new title
$my_post = array(
'ID' => $post_id,
'post_title' => $custom_title,
'post_name' => $custom_title,

);

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

}
}
add_action('cred_save_data', 'my_save_data_action5’,10,2);

// END autotittel5

****************

I have 30 post forms on this site, and I am wondering if it is possible to make this work on several forms (spesific forms), rather than duplicating the code over and over again. (With tweeks to accomodate the spesific post form id)

Maybe something like this?:

******

function my_save_data_action($post_id, $form_data){
// Change your CRED Form "ID" accordingly below
if ($form_data['id']==1577,1578,1579,1580){

//Declare the content of your variables, change "your_custom_field_slug" accordingly
$custom_title = 'Rapportnummer ' . $post_id;

//collect data and define new title
$my_post = array(
'ID' => $post_id,
'post_title' => $custom_title,
'post_name' => $custom_title,

);

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

}
}
add_action('cred_save_data', 'my_save_data_action’,10,2);

// END autotittel5

#1351341

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Yes - To apply the same code on multiple form IDs you should try to change the if condition as given under:

For example:

// add your form IDs here
$forms = array( 1577,1578,1579,1580 );
if ( in_array($form_data['id'], $forms) ){ 
#1351607

Hi Minesh,
Could you provide the entire code? When I attempt to combine the old code with you addition I get a php line error.

The forms this should apply to is: 1577, 1580, 1506 and 1284

#1351609

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Please try to use the following code:

function my_save_data_action5($post_id, $form_data){
// Change your CRED Form "ID" accordingly below

$forms = array( 1577,1580,1506,1284);
if ( in_array($form_data['id'], $forms) ){ 

//Declare the content of your variables, change "your_custom_field_slug" accordingly
$custom_title = 'Rapportnummer ' . $post_id;

//collect data and define new title
$my_post = array(
'ID' => $post_id,
'post_title' => $custom_title,
'post_name' => $custom_title,

);

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

}
}
add_action('cred_save_data', 'my_save_data_action5',10,2);
#1351621

Excellent support, as always!