Like this ticket : https://toolset.com/forums/topic/cred-form-title-auto-fill/
I create a post form and I want the product name populate with product category and a custom field (ville-d-arrivee) within the CRED Post form.
So i tried this code :
add_action('cred_save_data', 'my_custom_save_data_action', 10, 2);
function my_custom_save_data_action( $post_id, $form_data ){
// if a specific form
if ($form_data['id']==763) {
if(!empty($_POST['wpcf-product_cat'])){
$depart = $_POST['wpcf-product_cat'];
}
if(!empty($_POST['wpcf-ville-d-arrivee'])){
$arrivee = $_POST['wpcf-ville-d-arrivee'];
}
$title = $depart .' '. $arrive;
$slug = sanitize_title($title);
wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
}
}
Here a bit of my CRED form
[credform class='cred-form cred-keep-original']
[cred_field field='form_messages' value='' class='alert alert-warning']
<div class="row">
<div class="col-sm-6">
<b><p>Départ</p></b>
[cred_field field='product_cat' display='select' single_select='true' output='bootstrap']
</div>
<div class="col-sm-6">
<b><p>Arrivée</p></b>
[cred_field field='ville-d-arrivee' display='select' single_select='true' output='bootstrap']
</div>
</div>
Infortunately this is not working.
Could you help me ?
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
Hello. Thank you for contacting the Toolset support.
Well - you write almost proper code but to get the selected product category name you need to use wp_get_post_terms() function.
So, your code should look like:
add_action('cred_save_data', 'my_custom_save_data_action', 10, 2);
function my_custom_save_data_action( $post_id, $form_data ){
$title = get_the_title($post_id);
// if a specific form
if ($form_data['id']==763) {
if(!empty($_POST['product_cat'])){
$term_list = wp_get_post_terms($post_id, 'product_cat', array("fields" => "names"));
$depart = join("-",$term_list);
}
if(!empty($_POST['wpcf-ville-d-arrivee'])){
$arrive = $_POST['wpcf-ville-d-arrivee'];
}
$title = $title.' '.$depart .' '. $arrive;
$slug = sanitize_title($title);
wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
}
}
I've adjusted the code as you can see above, could you please confirm it works for you as well.
i used wp_get_post_terms() for the second field too cause it's a custom taxonomy and it's works well
I tried to add the custom field " date-de-depart" but it's not working.
add_action('cred_save_data', 'my_custom_save_data_action', 10, 2);
function my_custom_save_data_action( $post_id, $form_data ){
$title = get_the_title($post_id);
// if a specific form
if ($form_data['id']==763) {
if(!empty($_POST['product_cat'])){
$term_list = wp_get_post_terms($post_id, 'product_cat', array("fields" => "names"));
$depart = join("-",$term_list);
}
if(!empty($_POST['product_cat'])){
$term_list = wp_get_post_terms($post_id, 'ville-d-arrivee', array("fields" => "names"));
$arrivee = join("-",$term_list);
}
if(!empty($_POST['wpcf-date-de-depart'])){
$date = $_POST['wpcf-date-de-depart'];
}
$title = $depart .' => '.$arrivee . ' le '. $date;
$slug = sanitize_title($title);
wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
}
}
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
is "wpcf-date-de-depart" is a Types date field? If Yes, Types date field stores its value as timestamp.
You need to covert timestamp to date. Please try to use following code:
add_action('cred_save_data', 'my_custom_save_data_action', 10, 2);
function my_custom_save_data_action( $post_id, $form_data ){
$title = get_the_title($post_id);
// if a specific form
if ($form_data['id']==763) {
if(!empty($_POST['product_cat'])){
$term_list = wp_get_post_terms($post_id, 'product_cat', array("fields" => "names"));
$depart = join("-",$term_list);
}
if(!empty($_POST['product_cat'])){
$term_list = wp_get_post_terms($post_id, 'ville-d-arrivee', array("fields" => "names"));
$arrivee = join("-",$term_list);
}
if(!empty($_POST['wpcf-date-de-depart'])){
$date = get_post_meta($post_id,'wpcf-date-de-depart',true);
$date = date('Y-m-d',$date);
}
$title = $depart .' => '.$arrivee . ' le '. $date;
$slug = sanitize_title($title);
wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
}
}
You can change the date format as per your requirement. As you can see I've setup 'Y-m-d' format for now.
Could you please confirm it works as your end as well.
I will adjust the date format but works fine !! Thanks you. 😉