Home » Functions » sanitize_title

Function Name: sanitize_title

WordPress provides a handy function called sanitize_title that is used to sanitize a string and generate a URL-friendly slug. This function takes a string as input and removes any special characters or spaces, replacing them with dashes (-). It ensures that the resulting slug is safe to use in URLs and does not contain any invalid characters.

The sanitize_title function is commonly used when creating permalinks or generating user-friendly URLs for posts, pages, or custom content types. It helps in creating search engine optimized URLs that are easy to read, understand, and share.

Example Usage:

Let’s say we have a blog post title "Hello World, Welcome to WordPress!" that we want to convert into a URL-friendly slug. We can use the sanitize_title function to achieve this:

$title = "Hello World, Welcome to WordPress!";
$slug = sanitize_title($title);

After using the sanitize_title function, the $slug variable will contain the sanitized version of the title: "hello-world-welcome-to-wordpress". This slug can now be used as a part of the permalink or the URL of the blog post, making it more user-friendly and search engine optimized.

It’s important to note that sanitize_title does not handle internationalization or transliteration. If you are working with non-ASCII characters or need to convert accented characters into their ASCII equivalents, you should consider using the remove_accents function in conjunction with sanitize_title.

Overall, the sanitize_title function is a powerful tool in WordPress development for generating URL-friendly slugs and creating user-friendly permalinks. By incorporating this function into your code, you can ensure that your website has clean and SEO-friendly URLs.

Learn More on

Register an account to save your snippets or go Pro to get more features.