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.
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
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?
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' );