Skip Navigation

[Resolved] I need to generate in backend title with custom type field

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

Problem: I would like to automatically generate a post title from a custom field value when a User submits a Form to create a new post

Solution: Use the following custom code snippet to automate the post title:

add_action('cred_save_data', 'tssupp_build_title', 99, 2);
function tssupp_build_title($post_id, $formdata)
{
  if ($formdata['id']==4684) {
    if (isset($_POST['wpcf-nombre_del_comercio'])) {
     $new_title = $_POST['wpcf-nombre_del_comercio'];
     $post_update = array(
      'ID' => $post_id,
      'post_title' => $new_title
     );
     wp_update_post($post_update);
    }
  }
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://developer.wordpress.org/reference/functions/wp_update_post/
https://toolset.com/documentation/customizing-sites-using-php/functions/

This support ticket is created 4 years, 2 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1844227

Hi!

I have created a form to create business cards, and I need the title of that form in the backend to be the one I have in the form as the business title.

Can you help me?

#1844961

Hi, I can show you examples of custom code that can be used to generate the post title automatically for Form submission posts. You can customize the code for your needs. The code should be added in your child theme's functions.php file, or to a new custom code snippet in Toolset > Settings > Custom Code.

Here's an example:

// Update post title using custom field value
add_action('cred_save_data', 'tssupp_build_title', 10, 2);
function tssupp_build_title($post_id, $formdata)
{
  if ($formdata['id']==12345) {
    if (isset($_POST['wpcf-field-slug'])) {
     $new_title = $_POST['wpcf-field-slug'];
     $post_update = array(
      'ID' => $post_id,
      'post_title' => $new_title
     );
     wp_update_post($post_update);
    }
  }
}

You must replace 12345 with the numeric ID of this Form, which can be found in Toolset > Post Forms. You must also replace field-slug with the slug of the business title field. You can find this slug in Toolset > Custom Fields > Post Fields tab. You must include the wpcf- prefix here. So if your field slug in wp-admin is business-title, the code must be wpcf-business-title, for example.

Let me know if you have questions about this.

#1845025

Hi Christian,

I generate this code but doesnt work. Where is my problem?

//TITULO POR CUSTOM FIELD
//
// Update post title using custom field value
add_action('cred_save_data', 'tssupp_build_title', 10, 2);
function tssupp_build_title($post_id, $formdata)
{
  if ($formdata['id']==4684) {
    if (isset($_POST['wpcf-nombre_del_comercio'])) {
     $new_title = $_POST['wpcf-nombre_del_comercio'];
     wp_update_post($post_id, 'post_title', $new_title);
    }
  }
}
#1845043

I see, you may need to increase the priority in this case. Try setting the priority to 30 instead of 10:

add_action('cred_save_data', 'tssupp_build_title', 30, 2);

If this does not solve the problem I will need to take a closer look.

#1845057

I've changed the priority and it still doesn't work

#1845083

May I log into your admin area and see how this is set up? Please let me know where I can find this Form on the front-end of your site, and I will review your site configurations. Private login fields are available here.

#1845107

Okay I see the problem, I made an error in my code. Sorry for the confusion. I have updated my example, here is the functioning code you can use:

add_action('cred_save_data', 'tssupp_build_title', 99, 2);
function tssupp_build_title($post_id, $formdata)
{
  if ($formdata['id']==4684) {
    if (isset($_POST['wpcf-nombre_del_comercio'])) {
     $new_title = $_POST['wpcf-nombre_del_comercio'];
     $post_update = array(
      'ID' => $post_id,
      'post_title' => $new_title
     );
     wp_update_post($post_update);
    }
  }
}
#1845109

My issue is resolved now. Thank you!

#1852581

Hi Christian,

I try to repeat this code to another customize title

//TITULO BONOS

add_action('cred_save_data', 'bono_build_title', 99, 2);
function tssupp_build_title($post_id, $formdata)
{
  if ($formdata['id']==5908) {
    if (isset($_POST['wpcf-referencia-del-bono'])) {
     $new_title = $_POST['wpcf-referencia-del-bono'];
     $post_update = array(
      'ID' => $post_id,
      'post_title' => $new_title
     );
     wp_update_post($post_update);
    }
  }
}

But wordpress says it is wrong