Converting titles to URL-friendly strings in WordPress with sanitize_title_with_dashes

The sanitize_title_with_dashes function in WordPress is a built-in function that converts a title into a URL-friendly string. This function primarily takes a string as input and then performs several operations on it to make it suitable for use in a URL.

The function first removes HTML tags and invalid characters from the string. After this, it converts all the spaces and certain special characters into hyphens. It also ensures that there are no consecutive hyphens in the string, and trims any hyphens from the start and end of the string. The output is a string that is safe to use in a URL, as it only contains lowercase letters, numbers, and hyphens.

The sanitize_title_with_dashes function can be useful in a variety of situations. For instance, it can be used when creating a new post or page in WordPress, to generate a URL-friendly version of the title. It can also be used when creating custom post types or taxonomies, to ensure that the slugs used are URL-friendly. Additionally, it can be used in plugins or themes, to sanitize user input or other data that needs to be included in a URL.

Parameters

The sanitize_title_with_dashes function in WordPress accepts three parameters. These include:

  • $title (string, required): This is the title that needs to be sanitized.
  • $raw_title (string, optional, default value is ”): This parameter is not utilized in the function.
  • $context (string, optional, default value is ‘display’): This parameter defines the context in which the string is sanitized. When the value is set to ‘save’, extra entities are either converted to dashes or removed completely. The default value is ‘display’.

Return Value

The sanitize_title_with_dashes function returns a string. This string is the sanitized version of the input title.

If the function does not accept any parameters, it will be explicitly stated that the function does not require any parameters.

Examples

Example 1: How to sanitize a title with dashes

$title = "Hello World! This is a test title.";
$clean_title = sanitize_title_with_dashes($title);
echo $clean_title;

This code snippet takes a string stored in the $title variable, sanitizes it with the sanitize_title_with_dashes() function, and stores the result in the $clean_title variable. It then outputs the sanitized title. The function replaces spaces and special characters with dashes, making the title URL friendly.

Example 2: How to sanitize a title with dashes within a function

function create_slug($title) {
 $slug = sanitize_title_with_dashes($title);
 return $slug;
}
echo create_slug("Hello World! This is a test title.");

In this example, a function named create_slug() is created that takes a title as an argument, sanitizes it with the sanitize_title_with_dashes() function, and returns the sanitized title. The sanitized title is then outputted by calling the create_slug() function with a string as its argument.

Example 3: How to sanitize a title with dashes in a loop

$titles = array("Hello World!", "This is a test title.", "Another test title!");
foreach ($titles as $title) {
 $clean_title = sanitize_title_with_dashes($title);
 echo $clean_title . "<br>";
}

In this example, an array of titles is created. A foreach loop is used to iterate through each title in the array. Each title is sanitized with the sanitize_title_with_dashes() function and the sanitized title is outputted. This is useful for sanitizing multiple titles at once.

Conclusion

The sanitize_title_with_dashes function in WordPress is a tool that converts a given string into a URL-friendly format. It operates by replacing spaces and certain other characters with hyphens, removing certain special characters, and converting all characters to lowercase. This function is typically used for creating clean, readable URLs, often called ‘slugs’, which are crucial for SEO and user-friendly navigation. It’s an integral part of WordPress’s built-in functionality for managing and generating URLs for posts, pages, and other types of content.

Related WordPress Functions