你需要准确地使用wp_insert_post_data
. 这包含在WordPress完成所有验证/清理后将存储到数据库中的post数据数组。
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;
}
希望有帮助。