1

I need modify the sanitize of slug when a term is created. For example I'm creating a term with name 0,5 and the slug automatically generated is 05. I need to add a dash in the position of the comma, the spected slug will be 0-5.

It's possible to do it overridind the code in theme functions.php?

SOLVED:

add_action('wp_insert_term_data', 'slug_save_term_callback', 10, 3 );
function slug_save_term_callback($data, $taxonomy, $args) {
    $name = $data['name'];
    $name = str_replace(',', '-', $name);

    $data['slug'] = $name;

    return $data;

}

Thanks

1

You will have bugs like this. because you are not sanitizing the title with sanitize_title().

And you are not checking for duplications with wp_unique_term_slug()

Bugs tests:

  1. Add spaces in the name and see the slug with spaces.
  2. After you add the sanitize_title() add term with comma 5,5 add term with space 5 5 results with duplications.

So just fix this like this:

add_action('wp_insert_term_data', 'slug_save_term_callback', 10, 3 );
function slug_save_term_callback($data, $taxonomy, $args) {
    $name = $data['name'];
    $name = wp_unique_term_slug(sanitize_title(str_replace(',', '-', $name)), (object) $args);

    $data['slug'] = $name;

    return $data;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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