Skip Navigation

[Resolved] Submitting/editing content with CRED and changing post title and slug

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

Problem:
How to redirect to specific page or post after submit the CRED form.

Solution:
You can use CRED redirection hook: cred_success_redirect to redirect the post/post as per your need.

You can find proposed solution, in this case, with the following reply
https://toolset.com/forums/topic/submitting-editing-content-with-cred-and-changing-post-title-and-slug/#post-765606

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect

This support ticket is created 6 years, 6 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 3 replies, has 2 voices.

Last updated by Minesh 6 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#763772

Hi,

I am trying to change post title and slug upon submitting/editing a content via CRED form.
The below function doesn't work, but it works when I submit/edit via WordPress back-end:

function update_post_slug( $post_id ) {
    $post_type = get_post_type( $post_id );
    if ( $post_type == 'test' ) {
        remove_action( 'save_post', 'update_post_slug' );
        $updated_data = array(
            'ID' => $post_id,
            'post_title' => wp_strip_all_tags( $_POST[ 'wpcf' ][ 'title' ] ),
            'post_name' => ''
        );
        wp_update_post( $updated_data );
        add_action( 'save_post', 'update_post_slug' );
    } else {
        return;
    }
}
add_action( 'save_post', 'update_post_slug' );

The save_post hook doesn't work when using CRED?

I used the other code:

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

It works, but after the post is updated and the form redirection is triggered (I use ajax and upon editing user should see a message instead of the form), the page displays "not found". The url (the post slug part) doesn't change so this is actually not surprising as the slug has been updated, but is it actually expected behavior? I am probably missing something.

#765606

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - in this case - what if you try to use the CRED redirection hook: cred_success_redirect

And use following code where we use sanitize_title for post_name field:

function update_post_slug( $post_id, $form_data ) {
    if ( $form_data[ 'id' ] == 241 ) {
        $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 );

add_filter('cred_success_redirect', 'custom_redirect',10,3);
function custom_redirect($url, $post_id, $form_data){
 $custom_title = get_post_meta( $post_id, 'wpcf-title', true );
 $custom_title = sanitize_title($custom_title);

    if ($form_data['id']==241) {
        $url = '<em><u>hidden link</u></em>';
    }
    return $url;
}

Where:
- Adjust the return URL as per your need

More info:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect

#766993

Hi Minesh,

I'll give it a try, thank you.

However, I need this functionality also from the back-end. When I the following function is present too:

function update_post_slug( $post_id ) {
    $post_type = get_post_type( $post_id );
    if ( $post_type == 'test' ) {
        remove_action( 'save_post', 'update_post_slug' );
        $updated_data = array(
            'ID' => $post_id,
            'post_title' => wp_strip_all_tags( $_POST[ 'wpcf' ][ 'title' ] ),
            'post_name' => ''
        );
        wp_update_post( $updated_data );
        add_action( 'save_post', 'update_post_slug' );
    } else {
        return;
    }
}
add_action( 'save_post', 'update_post_slug' );

the other one called on "cred_save_data" doesn't work. Is the solution here to use is_admin() in the "update_post_slug" function to trigger it only from the back-end or there is a better way?

#767032

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - if you want to trigger it for only admin section - you should wrap the code with if condition having check with is_admin().

For example:

function update_post_slug( $post_id ) {
    $post_type = get_post_type( $post_id );
    if ( $post_type == 'test'  and is_admin()) {
        remove_action( 'save_post', 'update_post_slug' );
        $updated_data = array(
            'ID' => $post_id,
            'post_title' => wp_strip_all_tags( $_POST[ 'wpcf' ][ 'title' ] ),
            'post_name' => ''
        );
        wp_update_post( $updated_data );
        add_action( 'save_post', 'update_post_slug' );
    } else {
        return;
    }
}
add_action( 'save_post', 'update_post_slug' );