Support » Developing with WordPress » Modify term slug before saving

  • Resolved theblackjt

    (@theblackjt)


    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

    • This topic was modified 3 months, 1 week ago by  theblackjt. Reason: code formatted
Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    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.

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

    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.

    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.

    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.

    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)
  • You must be logged in to reply to this topic.