I have a site where YouTube video IDs are used as a post’s title. This mostly works fine, except some valid YouTube video IDs have two consecutive dashes. For example, “j–c0wP54JU” is getting automatically modified by wordpress to be “j-c0wP54JU” (Note the single dash).

When I try to manually edit the permalink from the Edit Post page, it keeps removing the 2 consecutive dashes when I press OK.

Where can I disable this part of WordPress that changes the URLs to be “web safe”?

2 Answers
2

Regarding the why part, I think I’ve traced this to the following line:

$title = preg_replace('|-+|', '-', $title);

within the sanitize_title_with_dashes() function.

It’s added to the santize_title() via the filter:

add_filter( 'sanitize_title', 'sanitize_title_with_dashes', 10, 3 );

but I wouldn’t recommend removing it totally.

Update:

Here’s a quick and dirty way around this:

add_filter( 'sanitize_title', function( $title )
{
    return str_replace( '--', 'twotempdashes', $title );
},9 );


add_filter( 'sanitize_title', function( $title )
{
    return str_replace( 'twotempdashes', '--', $title );
},11 );

where we replace two dashes into some temporary string, before the sanitize_title_with_dashes is activated and then replace the string again to two dashes afterwards. We could adjust this to the more general case, but you get the idea 😉

Tags:

Leave a Reply

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