How To Remove Case Sensitiveness Issue From Post Link In WordPress?

Resolving Case Sensitivity Issues in WordPress Post Links

When updating a post in WordPress, you may notice that the post link (permalink) is automatically converted to lowercase, even if it originally contained a mix of uppercase and lowercase letters. This article explains why this happens and provides a solution to preserve case sensitivity in post links.

The Issue

The case sensitivity issue arises due to the sanitize_title() function in WordPress, located in the wp-includes/formatting.php file. This function processes the post title to generate a URL-friendly slug, converting it to lowercase by default.

For example, a post link like MyPostTitle becomes myposttitle after an update, as shown in the screenshot below:

Post link in lowercase

Cause of the Issue

The sanitize_title() function is responsible for this behavior. Below is the original function structure:

function 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;

    return $title;
}

The function processes the title and returns it in lowercase, causing the case sensitivity issue.

Solution

To preserve the original case of the post title in the permalink, modify the sanitize_title() function to return $raw_title instead of $title. Below is the updated function:

function 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;

    return $raw_title;
}

Important Notes:

  • Do Not Modify Core Files Directly: Editing wp-includes/formatting.php is not recommended, as WordPress updates will overwrite your changes. Instead, use a custom plugin or the functions.php file in your theme to override the sanitize_title filter.
  • Alternative Approach: You can hook into the sanitize_title filter to preserve case sensitivity without modifying core files. Add the following code to your theme’s functions.php:
add_filter('sanitize_title', function($title, $raw_title, $context) {
    if ($context === 'save') {
        return $raw_title;
    }
    return $title;
}, 10, 3);

This filter checks if the context is save and returns the original $raw_title to maintain the case, while allowing other contexts to function normally.

Conclusion

By modifying the sanitize_title() function or using the sanitize_title filter, you can prevent WordPress from converting post links to lowercase, preserving the desired case sensitivity. Always test changes in a staging environment before applying them to a live site, and avoid direct edits to core WordPress files to ensure compatibility with future updates.

Related posts