How to hook into permalink when publishing-saving post?

I want to know how I can change the structure of the permalink to my desired one before saving or publishing my post in WordPress?
For instance, when I add the title like the wordpress blog in my post I get the permalink structure similar to the below:

http://localhost/2015/09/10/the-wordpress-blog/

I want to change it to something like below before saving or publishing it:

http://localhost/2015/09/10/the-wordpress-blog-is-mine/

But I don’t know hook what into what to achieve my goal.

2 Answers
2

Finally, I found my answer on my own.

//add our action
add_action( 'save_post', 'my_save_post', 11, 2 );

function my_save_post($post_id, $post){

   //if it is just a revision don't worry about it
   if (wp_is_post_revision($post_id))
      return false;

   //if the post is an auto-draft we don't want to do anything either
   if($post->post_status != 'auto-draft' ){

       // unhook this function so it doesn't loop infinitely
       remove_action('save_post', 'my_save_post' );

      //this is where it happens -- update the post and change the post_name/slug to the post_title
      wp_update_post(array('ID' => $post_id, 'post_name' => str_replace(' ', '-', $_POST['post_title'])));

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

Leave a Comment