Last active
June 6, 2021 16:29
-
-
Save Jany-M/a476a3eb6df59dc22faaf6188484af97 to your computer and use it in GitHub Desktop.
[WordPress] Automatically rename post slug on post save
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Change & Save New Permalink if post title was changed | |
function update_slug_on_edit( $data, $postarr ) { | |
global $post; | |
$id = $post->ID; | |
$status = $post->post_status; | |
$cpt = get_post_type($id); | |
$parent = $post->post_parent; | |
$title = $data['post_title']; | |
$partial_slug = sanitize_title($title); | |
$new_slug = wp_unique_post_slug($partial_slug, $id, $status, $cpt, $parent ); | |
if (!wp_is_post_revision($id)) { | |
$data['post_name'] = $new_slug; | |
} | |
return $data; | |
} | |
add_filter( 'wp_insert_post_data', 'update_slug_on_edit', 99, 2 ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment