How can I automatically remove certain words from the automatically generated permalink (it uses %postname%).

For example: if a new post is created with the title:

How to remove certain words from the Wordpress permalink

I want to have an array of forbidden words like: certain, permalink And then the slug would be saved automatically as as:


I don't want to affect old posts URLs just the new published ones.

I found plugins but they are too heavy, with millions of functions I just want to know how to modify the permalink while saving the post.


1 Answer 1


To update the slug of posts when they're published, hook to save_post action as in this answer. Also use sanitize_title() to perform the needed sensitization:

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' );

  • 1
    Thanks so much! it works. One question, all the sanitation done to the title (lowercase, trimming, replacing weird characters) is the same WordPress does by default or just guessing what it does? Feb 22, 2019 at 14:11
  • Good question @MichaelRogers! Actually, it's not the Wordpress way, I just copied it from another answer. There's another simpler Wordpressian way to do it. I'll edit the answer. Feb 22, 2019 at 15:50
  • 1
    I think this aproach is better: wordpress.stackexchange.com/a/102096/77283 Hooked to "sanitize_title". WP does all the sanitizing and we just remove the array words. The changes are seen live on the editor no need to save the post. Feb 22, 2019 at 16:46
  • That's great! Thanks for sharing this information. Did you test it? Feb 24, 2019 at 7:06
  • 1
    Yup, tested both yours and his, both work. but for example yours has a drawback. If you edit an old post that had a custom slug it will just replace it with the title with dashes. While the linked method will not touch it, only remove array words if present. Feb 25, 2019 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.