[wp-trac] [WordPress Trac] #58616: Replace apostrophe with hyphen for page slugs

WordPress Trac noreply at wordpress.org
Sun Jun 25 15:21:19 UTC 2023


#58616: Replace apostrophe with hyphen for page slugs
--------------------------+-----------------------------
 Reporter:  anrghg        |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Formatting    |    Version:
 Severity:  major         |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 I wonder if anybody ever complained about WordPress deleting apostrophes
 when generating slugs, unlike news outlets replacing them with hyphens for
 readability.

 Title-derived slugs are designed for human readability, but WordPress
 defeats the point by removing all apostrophes. In the resulting mess,
 genitives appear like plurals, it’s becomes its, and in other locales even
 more confusing or ridiculous constructs build up like when “l’a” (has it)
 becomes “la” (the, in French).

 Letter apostrophe and ʻokina need to become underscores along the same
 lines.

 A solution is to add the following near the start of
 `sanitize_title_with_dashes()` in `wp-includes/formatting.php`:
 {{{#!php
 <?php
 /**
  * Converts punctuation apostrophe to hyphen-minus.
  */
 $p_s_title = str_replace(
         array(
                 '’',  // Punctuation apostrophe.
                 '\'', // ASCII apostrophe.
         ),
         '-',
         $p_s_title
 );

 /**
  * Converts letter apostrophe and ʻokina to underscore.
  */
 $p_s_title = str_replace(
         array(
                 'ʼ',  // Letter apostrophe.
                 'ʻ',  // ʻokina.
         ),
         '_',
         $p_s_title
 );
 }}}

 A current workaround consists in adding this code in a child theme’s
 `functions.php`:
 {{{#!php
 <?php
 /**
  * Filters a sanitized title string respecting apostrophes.
  *
  * @courtesy WordPress
  * @see sanitize_title()
  * @link
 https://developer.wordpress.org/reference/functions/sanitize_title/
  *
  * @param string $p_s_title     Sanitized title.
  * @param string $p_s_raw_title The title prior to sanitization.
  * @param string $p_s_context   The context for which the title is being
 sanitized.
  */
 add_filter(
         'sanitize_title',
         function( $p_s_title, $p_s_raw_title, $p_s_context = 'save' ) {
                 if ( '' === $p_s_raw_title || false === $p_s_raw_title ) {
                         $p_s_raw_title = $p_s_title; // Already
 $fallback_title.
                 }
                 $p_s_title = $p_s_raw_title;

                 /**
                  * Converts punctuation apostrophe to hyphen-minus.
                  */
                 $p_s_title = str_replace(
                         array(
                                 '’',  // Punctuation apostrophe.
                                 '\'', // ASCII apostrophe.
                         ),
                         '-',
                         $p_s_title
                 );

                 /**
                  * Converts letter apostrophe and ʻokina to underscore.
                  */
                 $p_s_title = str_replace(
                         array(
                                 'ʼ',  // Letter apostrophe.
                                 'ʻ',  // ʻokina.
                         ),
                         '_',
                         $p_s_title
                 );

                 if ( 'save' === $p_s_context || 'display' === $p_s_context
 ) {
                         $p_s_title = remove_accents( $p_s_title,
 $p_s_locale = 'fr_FR' );
                         $p_s_title = sanitize_title_with_dashes(
 $p_s_title, '', $p_s_context );
                 }

                 return $p_s_title;
         },
         PHP_INT_MAX,
         3
 );
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/58616>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list