Title: Modify term slug before saving
Last modified: January 10, 2019

---

# Modify term slug before saving

 *  Resolved [theblackjt](https://wordpress.org/support/users/theblackjt/)
 * (@theblackjt)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/modify-term-slug-before-saving/)
 * I have the problem that terms like ‘c’, ‘c++’ and ‘c#’ are only saved once with‘
   c’ as slug. Based on example #2 from the answer on the thread linked belows I
   built some code that works fine when creating a term with its creation form. 
   But when I use the new post form to add a new term and save a the new post it
   doesn’t work. In this case the code is not being called. Some help would be nice.
 *     ```
       function pta_projekt_encode_term_slug( $str ) {
           $search = array("+", "#", '.net', '&', "/");
           $replace = array("plus", "sharp", 'dotnet', 'and', " ",);
           $str = str_replace($search, $replace, $str);
           return $str;
       }
       add_filter( 'pre_insert_term', function( $term, $taxonomy ) {
       add_filter( 'pre_term_slug', function( $value ) use ( $term, $taxonomy ) {
           if( ! did_action( 'pre_term_slug' ) && $term ) {
               $value = sanitize_title( pta_projekt_encode_term_slug( $term ) );
           }
           return $value;
       } );
       return $term;
       }, 10, 2 );
       ```
   
 * [https://wordpress.stackexchange.com/questions/208351/set-taxonomy-slug-as-taxonomy-title](https://wordpress.stackexchange.com/questions/208351/set-taxonomy-slug-as-taxonomy-title)
    -  This topic was modified 7 years, 4 months ago by [theblackjt](https://wordpress.org/support/users/theblackjt/).
      Reason: code formatted

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/modify-term-slug-before-saving/#post-11074864)
 * Your code works for me on my site. I’m on the post edit screen, where I add a
   new category term using the category meta box of the block editor. Perhaps your
   theme or a plugin is corrupting one of the filter stacks somehow?
 * BTW, don’t replace a slash with a space, use a hyphen. Spaces are not allowed
   in slugs.
 *  [Howdy_McGee](https://wordpress.org/support/users/howdy_mcgee/)
 * (@howdy_mcgee)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/modify-term-slug-before-saving/#post-11074903)
 * This seems to work fine, I’ve tested it on my local install against WooCommerce
   product categories. In the `pre_insert_term` it’s just passing the term name 
   where you want to do your replacing. WordPress should already replace the `$search`
   keywords when sanitizing the term slug so any hook after that sanitization will
   be too late.
 * The below will modify the title if it contains certain keywords then it should
   let WordPress sanitize and create the slug as normal.
 *     ```
       /**
        * Replace keywords in term name
        * 
        * @param String $term 		- Submitted term name to modify
        * @param String $taxonomy 	- Term taxonomy slug to test against if necessary
        * 
        * @return String $term
        */
       add_filter( 'pre_insert_term', function( $term, $taxonomy ) {
   
       	$search 	= array( "+", "#", ".net", "&", "/", );
           $replace 	= array( " Plus", " Sharp", ' Dotnet', " and ", " ", );
       	$term 		= str_replace( $search, $replace, $term );
       	$term		= trim( $term );
   
           return $term;
   
       }, 10, 2 );
       ```
   
 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/modify-term-slug-before-saving/#post-11074980)
 * The problem with Howdy’s answer is that the term name is modified, and that is
   not desired. It doesn’t actually solve the question of how to have the term names
   with special characters and not have the terms collapse into the same slug when
   they are removed.
 *  Thread Starter [theblackjt](https://wordpress.org/support/users/theblackjt/)
 * (@theblackjt)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/modify-term-slug-before-saving/#post-11078293)
 * Hey guys,
 * thanks for the replies. It is like Joy said, the terms should keep their special
   characters.
 * One thing that could be relevant ist, that the terms belong to a taxonomy called‘
   Programmiersprache’ an is linked to a custom post type with “projekt”. The taxonomy
   and the custom post type are created with the plugin cptui. I forgot to mentoion
   it in my first post.
 *  [Howdy_McGee](https://wordpress.org/support/users/howdy_mcgee/)
 * (@howdy_mcgee)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/modify-term-slug-before-saving/#post-11082799)
 * This should get what you need:
 *     ```
       /**
        * Modify term slug with certain characters
        * 
        * @param Array $data = Array( 'name' => 'Term Name', 'slug' => 'term-slug', 'term_group' => 0 )
        * @param String $taxonomy
        * @param Array $args
        * 
        * @return Array $data
        */
       add_filter( 'wp_insert_term_data', function( $data, $taxonomy, $args ) {
   
       	$search 	= array( "+", "#", ".net", "&", "/", );
           $replace 	= array( " Plus", " Sharp", ' Dotnet', " and ", " ", );
       	$slug 			= str_replace( $search, $replace, $data['name'] ); // Replace original title
       	$slug			= sanitize_title( trim( $slug ) );	// Slugify title
       	$data['slug']	= wp_unique_term_slug( $slug, (object) $args );
   
       	return $data;
   
       }, 10, 3 );
       ```
   
 * The above keeps the title intact but uses the title to regenerate the slug based
   on the replacements.
 *  Thread Starter [theblackjt](https://wordpress.org/support/users/theblackjt/)
 * (@theblackjt)
 * [7 years, 4 months ago](https://wordpress.org/support/topic/modify-term-slug-before-saving/#post-11086645)
 * Hey Howdy_mcgee,
 * thanks a lot. That’s absolutely what I need. My problem is solved.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Modify term slug before saving’ is closed to new replies.

## Tags

 * [filter](https://wordpress.org/support/topic-tag/filter/)
 * [hook](https://wordpress.org/support/topic-tag/hook/)
 * [slug](https://wordpress.org/support/topic-tag/slug/)
 * [taxonomy](https://wordpress.org/support/topic-tag/taxonomy/)
 * [terms](https://wordpress.org/support/topic-tag/terms/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 6 replies
 * 4 participants
 * Last reply from: [theblackjt](https://wordpress.org/support/users/theblackjt/)
 * Last activity: [7 years, 4 months ago](https://wordpress.org/support/topic/modify-term-slug-before-saving/#post-11086645)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
