Skip Navigation

[Resolved] CRED Create Title from Entered Fields

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

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 2 voices.

Last updated by vijayB-4 6 years, 12 months ago.

Assisted by: Luo Yang.

Author
Posts
#514800

I am trying to: Create an Auto Tile in a CRED form

I have used the following code

add_action('cred_save_data', 'build_title', 10, 2);

function build_title($post_id, $formdata){
if ($form_data['id']==12033)
{
$parent_id1 = get_post_meta($post_id, '_wpcf_belongs_swimmer_id', true);
$field1 = get_post_meta($parent_id1, 'post_title', true);
$parent_id2 = get_post_meta($post_id, '_wpcf_belongs_swim-event_id', true);
$field2 = get_post_meta($parent_id2, 'post_title', true);
$field3 = get_the_terms( $post_id, 'swim-race' );

// $field3 = get_post_meta($post_id, 'swim-race');

$title = $field1 .' '. $field2 .' '.$field3;
$slug = sanitize_title($title);
wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
}
}

I expected to see: The proper Title

Instead, I got:

CRED Auto Draft 47d6095db28e49f356da8469176dd08f as title

Where Am I Going Wrong?

CRED Form Code
[credform class='cred-form cred-keep-original']

[cred_field field='form_messages' value='']

<div class="cred-group cred-group-parents">
<div class="cred-field cred-field-_wpcf_belongs_swimmer_id">
<label class="cred-label">
Swimmer Name
</label>
[cred_field field='_wpcf_belongs_swimmer_id' value='']
</div>
<div class="cred-field cred-field-_wpcf_belongs_swim-event_id">
<label class="cred-label">
Meet name
</label>
[cred_field field='_wpcf_belongs_swim-event_id' value='']
</div>

</div>

<div class="cred-group cred-group-Swim-Timings">
<div class="cred-header"><h3>Swim Timings</h3></div>
<div class="cred-field cred-field-swim-time">
<label class="cred-label">
Swim time
</label>
[cred_field field='swim-time' post='swim-timing' value='' urlparam='']
</div>
<div class="cred-field cred-field-race-position">
<label class="cred-label">
Race position
</label>
[cred_field field='race-position' post='swim-timing' value='' urlparam='']
</div>
<div class="cred-field cred-field-new-meet-record">
<label class="cred-label">
New Meet Record
</label>
[cred_field field='new-meet-record' post='swim-timing' value='' urlparam='']
</div>
</div>

<div class="cred-group cred-group-taxonomies">
<div class="cred-taxonomy cred-taxonomy-swim-course">
<div class="cred-header"><h3>Swim Courses</h3></div>
[cred_field field='swim-course' display='checkbox']
<div class="cred-taxonomy-auxilliary cred-taxonomy-auxilliary-swim-course_add_new">
[cred_field field='swim-course_add_new' taxonomy='swim-course' type='add_new']
</div>
</div>
<div class="cred-taxonomy cred-taxonomy-swim-race">
<div class="cred-header"><h3>Swim Races</h3></div>
[cred_field field='swim-race' display='checkbox']
<div class="cred-taxonomy-auxilliary cred-taxonomy-auxilliary-swim-race_add_new">
[cred_field field='swim-race_add_new' taxonomy='swim-race' type='add_new']
</div>
</div>
<div class="cred-taxonomy cred-taxonomy-swimmer-event-group">
<div class="cred-header"><h3>Swimmer Event Groups</h3></div>
[cred_field field='swimmer-event-group' display='checkbox']
<div class="cred-taxonomy-auxilliary cred-taxonomy-auxilliary-swimmer-event-group_add_new">
[cred_field field='swimmer-event-group_add_new' taxonomy='swimmer-event-group' type='add_new']
</div>
</div>

</div>

[cred_field field='form_submit' value='Submit' urlparam='']

[/credform]

#515171

Dear Vijay,

I can not find anything wrong in your PHP codes, since it is a custom PHP codes problem, I suggest you check these:
1) the CRED form ID is 12033,
2) And you can also manually debug your PHP codes line by line
3) If the problem still exists, please provide a test site with same problem in below "private detail box", also point out the problem page URL and CRED form URL, and where I can edit your PHP codes, I need a live website to test and debug.

#515223

Thanks for the details, the variable name "$formdata" is wrong in your codes, I changed it from:

if ($form_data['id']==12033)

To:

if ($formdata['id']==12033)

And there is some other PHP errors in your codes, you will need to manually debug your codes line by line, for example:
$field3 = get_the_terms( $post_id, 'swim-race' );
the wordpress function get_the_terms() will return an array, you can not use it directly, see wordpress document:
https://developer.wordpress.org/reference/functions/get_the_terms/

#515668

Hi,

Still not working

Code now looks like this

add_action('cred_save_data', 'build_title', 10, 2);

function build_title($post_id, $formdata){
if ($formdata['id']==12033)
{
$parent_id1 = get_post_meta($post_id, '_wpcf_belongs_swimmer_id', true);
$field1 = get_post_meta($parent_id1, 'post_title', true);
$parent_id2 = get_post_meta($post_id, '_wpcf_belongs_swim-event_id', true);
$field2 = get_post_meta($parent_id2, 'post_title', true);
//$field3 = get_the_terms( $post_id, 'swim-race' );

//$field3 = get_post_meta($post_id, 'swim-race');

$title = $field1 .' '. $field2;
$slug = sanitize_title($title);
wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
}
}

Now The output (Title ) is blank

Can you advise.

Also how can I get the Taxonomy value entered for the post.

Regards
Vijay

#515735

I have modified the PHP codes as below, please test again, check if it is fixed:

add_action('cred_save_data', 'build_title', 10, 2);

function build_title($post_id, $formdata){
  if ($formdata['id']==12033)
{
	$title_arr = array();
	$parent_id1 = get_post_meta($post_id, '_wpcf_belongs_swimmer_id', true);
	$title_arr[] = get_the_title($parent_id1);
	$parent_id2 = get_post_meta($post_id, '_wpcf_belongs_swim-event_id', true);
	$title_arr[] = get_the_title($parent_id2);
	$swim_race = get_the_terms( $post_id, 'swim-race' );
	foreach($swim_race as $race){
	  $title_arr[] = $race->name;
	}
	$title = implode(' ', $title_arr);
	$slug = sanitize_title($title);
	wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
}
}

#515749

Dear Luo,
This is resolved.

Thank you very much

Vijay

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.