Rewrite post_name (slug) before publishing a post

  1. Dario BF


    Is a known (SEO) issue that if You save a draft post without title, the post_name or slug is immediately saved with the post id.

    Later, when You finish your post and put some title, the slug doesn't change; neither also when your slug preference is %postname%.

    So, what if we include the following function to rewrite the slug before publishing the post?

    * Rewrite post_name (slug) before publish it using
    * WordPress function.
    // initial hook
    add_action( 'save_post', 'rewrite_post_name' );

    function rewrite_post_name( $post_id ) {

    // verify post is not a revision
    if ( ! wp_is_post_revision( $post_id ) ) {

    // unhook this function to prevent infinite looping
    remove_action( 'save_post', 'rewrite_post_name' );

    $post_name = get_post_meta ( $post_id, 'post_name' );
    $post_id2 = $post_id . "-2";

    if( $post_name === $post_id || $post_name === $post_id2 ){
    // update the post slug
    wp_update_post( array(
    'ID' => $post_id,
    'post_name' => '' // Rewrite based on Post Title

    // re-hook this function
    add_action( 'save_post', 'rewrite_post_name' );


    Posted: 2 years ago #
  2. le mie nozze


    8 votes in two week! Good!

    Sounds like a good idea include the following function to rewrite the slug BEFORE publishing the post!

    Posted: 2 years ago #
  3. motorradhelm


    thank u so much!

    Posted: 2 years ago #
  4. saftausbeute


    I have had to encounter this exact issue several times before, which is why I'm absolutely supporting the idea, or rather the fix, you're suggesting here.
    Thank you, hopefully we'll see it being revised soon!

    Posted: 2 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.

  • Rating

    16 Votes
  • Status

    This idea has been implemented