I am trying to: I have an edit form for custom post that allows the User to change Post Title, but when they do, the permalink still reflects the previous post title name.
My permalink is set to /%postname%/
So I do not understand why the permalink does not change to the new Post Title name.
Hi, I think this is the standard behavior for WordPress. If you create a Post, publish it, then later change the title, the permalink is not automatically updated with a new slug. A custom post type behaves the same way. You would have to manually do that in wp-admin, or use custom code to update the permalink from Forms. A few other tickets here in the forum discuss similar custom code. Here's one you can use as a reference: https://toolset.com/forums/topic/submitting-editing-content-with-cred-and-changing-post-title-and-slug/
We have documentation for the Forms API available here: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
Let me know if you have questions about that.
Christian,
I added the following code to my functions.php file but for some reason there is a syntax error (shown in pic)
//Update Post Title
function update_post_slug( $post_id, $form_data ) {
$forms = array( 49, 1355 );
if (in_array($form_data['id'], $forms ) {
$custom_title = get_post_meta( $post_id, 'wpcf-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 );
This is the error it gives,
Your PHP code changes were rolled back due to an error on line 438 of file wp-content/themes/astra-child/functions.php. Please fix and try saving again.
syntax error, unexpected ';'
Not sure why it is giving that error because I don't think it should.
I also just tried using one form with the code below:
//Update Post Title
function update_post_slug( $post_id, $form_data ) {
if ( $form_data[ 'id' ] == 1355 ) {
$custom_title = get_post_meta( $post_id, 'wpcf-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 );
But this code erases the new title name from the form. Why is that?
The syntax error is here:
if (in_array($form_data['id'], $forms ) {
You're missing a closing parenthesis. The correct code should be:
if (in_array($form_data['id'], $forms ) ) {
I can't see the contents of your Form, but let's discuss this line:
$custom_title = get_post_meta( $post_id, 'wpcf-title', true );
This code would work if you have a custom field in Types with the slug "title", and you are using the value of this field to set the new post title. The get_post_meta function is for getting custom fields. But normally that isn't the case, normally you have only the standard post title field where the User can set a different title. It's not a postmeta field, and it doesn't use the wpcf- prefix. Instead you would get the post title using get_the_title and the new/edited post ID from the first parameter:
$custom_title = get_the_title( $post_id );
Here is the final code that worked for me:
//Update Post Title
function update_post_slug( $post_id, $form_data ) {
$forms = array( 49, 1355 ); //Add your form numbers here
if (in_array($form_data['id'], $forms )) {
$custom_title = 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 );
My issue is resolved now. Thank you!