Skip Navigation

[Resolved] Create Post Title Using Submitted Field Data

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.

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 4 replies, has 2 voices.

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

Assisted by: Shane.

Author
Posts
#1235891

We are developing a commercial photography scheduling and media delivery portal using Toolset. For scheduling, we have a form that logged-in users can submit to create a new task. This new task is a Custom Post Type with custom fields, setup using toolset.

Our form contains two fields, Model Name and Community Name. We would like for the name of the created post to be derived from the data entered into these two fields. For instance, the user enters "Radford" into the Model Name field, and "River Creek" into the Community Name field. When the custom post is created, it would have a title generated as "Radford at River Creek".

Is there a way to do this with through Toolset? Thanks!

#1235895

Shane
Supporter

Languages: English (English )

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

Hi Brad,

Thank you for contacting our support forum.

It is actually possible by using a Toolset forms hook.


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

The code above should be able to get this done for you. What you need to do is to replace the 'wpcf-field-slug-1' with the slug of your custom field keeping the wpcf- prefix.

Next just change XXXX to the ID of the form that you are using.

You can add the code to our Toolset custom code section in Toolset->Settings->custom code.

Just add the code and activate it.

Thanks,
Shane

#1235899

My issue is resolved now. Thank you!

#1235905

Hey Shane, getting an error. Did I mention I'm not a developer? Heh, I will probably eventually figure this out, but in case you guys answer before I do, here's the error:

[2019-04-25 22:39:58, admin] syntax error, unexpected '$field_2' (T_VARIABLE) in /home/cmpadm/public_html/wp-content/toolset-customizations/task_form_title.php on line 14

Here's the code, line 14 is where $field_2 starts:

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

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']==108) {
$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);
}
}

#1235906

Semicolons...disregard, and thanks again!