Skip Navigation

[Resuelto] Permalink should change to when post title changes

This support ticket is created hace 5 años, 4 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Este tema contiene 5 respuestas, tiene 2 mensajes.

Última actualización por FelipeP5703 hace 5 años, 4 meses.

Asistido por: Christian Cox.

Autor
Mensajes
#1278917

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.

#1278949

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.

#1281049
Update Title.jpg

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.

#1281063

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?

#1281189

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 );
#1281209

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!