要在发布帖子时更新这些帖子,请连接到save_post
操作如中所示this answer. 同时使用sanitize_title() 进行所需的致敏:
add_action( \'save_post\', \'wpse325979_save_post_callback\', 10, 2 );
function wpse325979_save_post_callback( $post_id, $post ) {
// verify post is not a revision
if ( ! wp_is_post_revision( $post_id ) ) {
// unhook this function to prevent infinite looping
remove_action( \'save_post\', \'wpse325979_save_post_callback\' );
$title = $post->post_title;
// remove forbidden keywords
$forbidden = array(\'~certain~i\', \'~permalink~i\'); //i modifier for case insensitive
$title = preg_replace($forbidden, \'\', $title);
// update the post slug
$slug = sanitize_title($title);
wp_update_post( array(
\'ID\' => $post_id,
\'post_name\' => $slug
));
// re-hook this function
add_action( \'save_post\', \'wpse325979_save_post_callback\' );
}
}