Using WordPress ‘pre_{$taxonomy}_{$field}’ PHP filter

The pre_{$taxonomy}_{$field} WordPress PHP filter allows you to modify a taxonomy field value before it is sanitized. The dynamic portions of the filter name, $taxonomy and $field, refer to the taxonomy slug and field name, respectively.

Usage

add_filter('pre_{$taxonomy}_{$field}', 'your_custom_function', 10, 1);
function your_custom_function($value) {
  // your custom code here
  return $value;
}

Parameters

  • $value (mixed): Value of the taxonomy field.

More information

See WordPress Developer Resources: pre_{$taxonomy}_{$field}

Examples

Modify category description before saving

In this example, we append a string to the category description before saving it.

add_filter('pre_category_description', 'append_text_to_description', 10, 1);
function append_text_to_description($value) {
  $value .= ' - Custom Text';
  return $value;
}

Add a prefix to custom taxonomy title

In this example, we add a prefix to a custom taxonomy (book_genre) title before saving.

add_filter('pre_book_genre_name', 'add_prefix_to_book_genre_title', 10, 1);
function add_prefix_to_book_genre_title($value) {
  $value = 'Genre: ' . $value;
  return $value;
}

Sanitize custom taxonomy (movie_rating) slug

In this example, we sanitize the custom taxonomy (movie_rating) slug before saving.

add_filter('pre_movie_rating_slug', 'sanitize_movie_rating_slug', 10, 1);
function sanitize_movie_rating_slug($value) {
  $value = sanitize_title($value);
  return $value;
}

Remove special characters from a tag name

In this example, we remove special characters from a tag name before saving.

add_filter('pre_post_tag_name', 'remove_special_chars_from_tag_name', 10, 1);
function remove_special_chars_from_tag_name($value) {
  $value = preg_replace('/[^a-zA-Z0-9\s]/', '', $value);
  return $value;
}

Convert custom taxonomy (recipe_type) description to uppercase

In this example, we convert the custom taxonomy (recipe_type) description to uppercase before saving.

add_filter('pre_recipe_type_description', 'convert_description_to_uppercase', 10, 1);
function convert_description_to_uppercase($value) {
  $value = strtoupper($value);
  return $value;
}