[wp-trac] [WordPress Trac] #47594: Add nbsp symbol codes to convert throught sanitize_title

WordPress Trac noreply at wordpress.org
Tue Feb 2 14:43:36 UTC 2021


#47594: Add nbsp symbol codes to convert throught sanitize_title
--------------------------------------+-----------------------------
 Reporter:  hokku                     |       Owner:  SergeyBiryukov
     Type:  enhancement               |      Status:  accepted
 Priority:  normal                    |   Milestone:  5.7
Component:  Formatting                |     Version:  5.3
 Severity:  minor                     |  Resolution:
 Keywords:  has-patch has-unit-tests  |     Focuses:
--------------------------------------+-----------------------------
Changes (by ocean90):

 * keywords:  has-patch has-unit-tests commit => has-patch has-unit-tests


Comment:

 Why are the changes part of `sanitize_title()` and not
 `sanitize_title_with_dashes()` which already handles some of the cases?

 The list also seems to be incomplete. I think if we're going to update the
 list we should cover all space characters. Here's a filter I have recently
 used (which is also incomplete):

 {{{#!php
 <?php
 /**
  * Extends sanitize_title_with_dashes() to replace more space related
 characters.
  *
  * @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.
  * @return string The sanitized string.
  */
 function trac_replace_spaces_in_titles( string $title, string $raw_title,
 string $context ): string {
         if ( 'save' !== $context ) {
                 return $title;
         }

         $title = str_replace(
                 [
                         '%e2%80%89', // THIN SPACE.
                         ' ', // THIN SPACE.
                         ' ', // THIN SPACE.
                         '%e2%80%8a', // HAIR SPACE.
                         '%e2%80%91', // NON-BREAKING HYPHEN.
                         '%e2%80%92', // FIGURE DASH.
                         '%e2%80%af', // NARROW NO-BREAK SPACE.
                         ' ', // NARROW NO-BREAK SPACE.
                 ],
                 '-',
                 $title
         );

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

         return $title;
 }
 add_filter( 'sanitize_title', 'trac_replace_spaces_in_titles', 10, 3 );
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/47594#comment:25>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list