Skip Navigation

[Resolved] After editing the title-field in a form, make sure slug is updated accordingly

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

Problem:

The customer wanted to update the post slug when the post title was updated using the Toolset post form.

Solution:

Shared the code example for the function attached to the "cred_save_data" hook.
( ref: https://toolset.com/forums/topic/after-editing-the-title-field-in-a-form-make-sure-slug-is-updated-accordingly/#post-2257177 )

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 2 years, 11 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/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by Fred Konings 2 years, 11 months ago.

Assisted by: Waqar.

Author
Posts
#2255981

Tell us what you are trying to do?
After editing and saving the title-field in a form (CPT = "Fotonieuws"), make sure slug is updated accordingly

I have found topic, which might be related.
https://toolset.com/forums/topic/submitting-editing-content-with-cred-and-changing-post-title-and-slug/

The form I use is:

[php]
[credform]
[cred_i18n type="html" name="scaffold_field_id"]<div style="text-align: right;"><h2>Fotonieuws</h2></div>[/cred_i18n]
[cred_field field='form_messages' class='alert alert-warning']
<div class="form-group">
<label for="%%FORM_ID%%_post_title">[cred_i18n name='post_title-label']Titel nieuwsbericht[/cred_i18n]</label>
[cred_field field='post_title' class='form-control' output='bootstrap']
</div>
<div class="form-group">
<label for="%%FORM_ID%%_post_content">[cred_i18n name='post_content-label']Beschrijving nieuwsbericht[/cred_i18n]</label>
[cred_field field='post_content' output='bootstrap']
</div>
<div class="form-group">
<label for="%%FORM_ID%%__featured_image">[cred_i18n name='_featured_image-label']Selecteer een foto (bijvoorkeur 1:1 ratio)[/cred_i18n]</label>
[cred_field field='_featured_image' output='bootstrap' previewsize='thumbnail']
</div>
[cred_field field='form_submit' output='bootstrap' value='Fotonieuws opslaan' class='btn btn-primary btn-lg']
[/credform]
[php]

Question: How to retrieve the value of the 'post-title' field from the form above, and use it in the function below?

[php]
function update_post_slug( $post_id, $form_data ) {
if ( $form_data[ 'id' ] == 597 ) {
$custom_title = get_post_meta( $post_id, 'post_title', true );
$updated_data = array(
'ID' => $post_id,
'post_title' => $custom_title,
'post_name' => sanitize_title($custom_title),
);
wp_update_post( $updated_data );
}
}
add_action( 'cred_save_data', 'update_post_slug', 10, 2 );

[php]

#2257177

Hi,

Thank you for contacting us and I'd be happy to assist.

To get the value of the current post's title, you can use the function "get_the_title" and here is the updated code that should work:


function update_post_slug( $post_id, $form_data ) {
	if ( $form_data[ 'id' ] == 597 ) {
		$custom_title = esc_html( get_the_title($post_id) );
		$updated_data = array(
			'ID' => $post_id,
			'post_title' => $custom_title,
			'post_name' => sanitize_title($custom_title),
		);
		wp_update_post( $updated_data );
	}
}
add_action( 'cred_save_data', 'update_post_slug', 10, 2 );

regards,
Waqar

#2257271

Hello Waqar,

The provided solution works as expected! Thank you very much!

My issue is resolved now. Thank you!