I would like to know what hook should I used when the permalink/url of a post is modified.
My goal is to get the old permalink and new permalink so I can used it for my plugin.
Thanks.
[EDIT]
Just want to clarify the question. I would like to get the old and new url in a scenario for example, when a user select one its post in the admin area, and edit the current permalink/url(/helloworld) into a new permalink/url(/helloworld_new) of the selected post.
I would like to get the complete url the post being edited.
2 Answers
You need to exactly use wp_insert_post_data
. This contains array of post data what will be store into database after WordPress has done all of the validation/sanitization.
add_filter('wp_insert_post_data', 'wpse_wp_insert_post_data', 10, 2);
function wpse_wp_insert_post_data($data, $post_attr)
{
// you get the post_name using $data['post_name'];
// post id will not be present for the first insert
// but you can check $post_attr['ID'] to be sure if an ID has been passed.
// note: $data won't contain post id ever, only the $post_attr will have it
// if you want to compare the name, you could use -
if( isset($post_attr['post_name'])
&& !empty($post_attr['post_name'])
&& $post_attr['post_name'] != $data['post_name'] )
{
// So here you can guess post name has been changed
// note: $post_attr['post_name'] might come undefined or empty sometime.
// and $data['post_name'] could also comes up empty, but it will be always defined
}
// you do not need to modify anything, so you should return it as it is
return $data;
}
Hope it helps.