Skip Navigation

[Resolved] Post Title Created from Form Fields

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted to create a custom post title from their custom fields.

Solution:

This can be done by using the Hook below as an example

add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {
if ($form_data['id']==XXXX) {
$field_1 = $_POST['wpcf-field-slug-1']
$field_2 = $_POST['wpcf-field-slug-2']
$args = array('ID' => $post_id, 'post_title' => $field_1.' '.$field_2);
wp_update_post($args);
}
}

Now in this user's case we are using 2 custom fields as you can see from teh $field_1 and $field_2 variable.

Add this to your toolset custom code in Toolset -> Settings -> Custom code and then activate it.

Notice you must change the XXXX to the ID of your form.

100% of people find this useful.

This support ticket is created 5 years, 8 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 7 replies, has 2 voices.

Last updated by bradC-8 5 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#1236311

I realize this request would be outside of the support realm for most companies. However, I also noticed that some Toolset supporters were providing customized code through lengthy support requests. I was hoping this would be a simple one, and should also mention we've started a search for a Toolset developer and are waiting to hear back from several.

Yesterday, Shane gave me the following code to construct a new custom post title based on form fields. It works great:

<?php
/**
* Creates post title based on form field values.
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {
if ($form_data['id']==116) {
$field_1 = $_POST['wpcf-vmtasks_modelname'];
$field_2 = $_POST['wpcf-vmtasks_community'];
$args = array('ID' => $post_id, 'post_title' => $field_1.' '.$field_2);
wp_update_post($args);
}
}

If you look at our form, located here:

hidden link

You'll see that we have a dropdown menu called Session Type, the selections for which change the displayed fields. It's those changing fields from which we want to construct our post title. If you select session type as "Model Home", the fields "Model Name" and "Community Name" are displayed. The content of those two fields determines the new custom post title.

If you select Community Amenities, only the additional "Community Name" field is displayed, from which we'd like to have the post title constructed as "Community Name" + Amenities. If you select "Other", we'd like to have the post title created as whatever the user inputs in that field.

Seems like some simple else/elseif stuff, but I can't get it working. Any code is appreciated.

#1236346

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Brad,

Could I'm unable to see the form since you didn't allow guest access 🙂

I should be able to get this working for you but i need to see the form.

Could you provide a simple account that is able to see the form or give guest access to the form?

Thanks,
Shane

#1236354

Bahh, sorry. I think it's available now. I can see it from other browsers when not logged in. Thanks!

#1236356

One thing I don't understand about this support system...how do I pass logins to you guys? It seems like these "tickets" are actually forum posts viewable by your other clients.

#1236387

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Brad,

So fo your case i'll show you an example with the Model Home .


if ($form_data['id']==116) {
if (isset($_POST['wpcf-vmtasks_type'])){
if($_POST['wpcf-vmtasks_type'] == "Model Home"){
$field_1 = $_POST['wpcf-vmtasks_modelname'];
$field_2 = $_POST['wpcf-vmtasks_community'];
$args = array('ID' => $post_id, 'post_title' => $field_1.' '.$field_2);
}
if($_POST['wpcf-vmtasks_type'] == "Regional Amenities"){
$field_1 = $_POST['wpcf-vmtasks_region'];
$args = array('ID' => $post_id, 'post_title' => $field_1);
}
}
wp_update_post($args);
}

Please let me know if this helps.
Thanks,
Shane

#1236415

Ohhh, thank you Shane. So much... I was keeping wp_update_post in each if statement. Will test and close shortly...man I appreciate this.

#1236416

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Brad,

Happy i'm able to guide you in the right direction 🙂

#1236490

My issue is resolved now. Thank you!