Slugify Any Text in WordPress with sanitize_title() Function

WordPressPosted on

There are some cases when we want to make a slug from an URL or text. In these cases, it is handy if we have a function which does the job.

A straightforward example is in-page anchor navigation where the sections can be updated dynamically. In this case, the easiest way to signify the title of a part and use it as an anchor.

$title = sanitize_title('Slugify Any Text in WordPress with sanitize_title() Function');

echo $title; // slugify-any-text-in-wordpress-with-sanitize_title-function

In WordPress, we can use the sanitize_title() function. The function does the followings:

  • strips the special characters,- converts all characters to lowercase,
  • strips HTML and PHP tags/code,
  • replaces whitespaces, underscores, periods with dashes,
  • removes consecutive dashes.

Usually, this will be useful for us but if not we can also extend it through its API. For more information and the parameters, please visit the official documentation.

Need a web developer? Maybe we can help, get in touch!

Similar Posts

More content in WordPress category