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?
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.
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);
}
}
}
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.
I've changed the priority and it still doesn't work
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.
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);
}
}
}
My issue is resolved now. Thank you!
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