Taxonomy Terms: Sanitizes a title or returns a fallback title with unique value or string

As a WordPress requirement, sometimes clients need to make consistency in a taxonomy terms slug and this can be done manually but it takes your time and it is not efficient.

WordPress sanitizes the title as a slug and that can be modified or customize as per the need.

In this case, we will add a unique string before every slug using the filter. We are using `pre_term_slug` filter which by default has 10 as a priority and 3 arguments.

Three arguments are:

  1. `string $title` which indicates the string to be sanitized.
  2. `string $fallback_title` Optional which indicates title to use if $title is empty.
  3. `string $context` Optional which indicates the operation for which the string is sanitized

WordPress Snippet: How to sanitizes a title, or returns a fallback title with unique value or string.

You need to play with below code snippet to add a unique string before every slug that will be sanitized.In this case, we will add `geekerhub` as a unique string before returning the sanitized title

Before

sanitizes title

/**
* Sanitizes a title, or returns a fallback title with unque value or string
*
* Specifically, HTML and PHP tags are stripped. Further actions can be added
* via the plugin API. If $title is empty and $fallback_title is set, the latter
* will be used.
*
* @since 1.0.0
*
* @param string $title The string to be sanitized.
* @param string $fallback_title Optional. A title to use if $title is empty.
* @param string $context Optional. The operation for which the string is sanitized
* @return string The sanitized string.
*/

function geekerhub_sanitize_title( $title, $fallback_title = '', $context = 'save' ) {
$raw_title = $title;

if ( 'save' == $context ) {
$title = remove_accents( $title );
}

/**
* Filters a sanitized title string.
*
* @since 1.2.0
*
* @param string $title Sanitized title.
* @param string $raw_title The title prior to sanitization.
* @param string $context The context for which the title is being sanitized.
*/
$title = apply_filters( 'sanitize_title', $title, $raw_title, $context );

if ( '' === $title || false === $title ) {
$title = $fallback_title;
}

$title = 'geekerhub'.$title;
return $title;
}

add_filter( 'pre_term_slug', 'geekerhub_sanitize_title' );

After

sanitizes a title

How to use this snippet in your WordPress Site?

This WordPress snippet can be placed at the bottom of the child theme’s functions.php before PHP close tag if exists OR you can place this snippet in your main theme’s functions.php if child theme does not exist. It is mandatory that you know how to edit such files.

Let us know whether snippets still work or not.

I would be happy if there is no issue with the snippet but would be happier if snippet does not work and you let me know. If snippet does not work then I will be enthusiastic to revise the snippets and come with the latest changes. You can let me know about snippet status via the comments section, contact us or via email address ‘iahmed964@gmail.com’

Leave a Comment

Your email address will not be published. Required fields are marked *