• Hi, I publish many articles, sometimes the permalink is not correct.
    I would like a title like “L’alba” to appear in the permalink as “l-alba”, but now “lalba” is now displayed.

    How can I solve the problem?
    Is there any code to do this without using plugins?

    Thanks

    • This topic was modified 6 years, 7 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not a Developing with WordPress topic
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    One would expect there to be a direct filter for this, but the only one I see requires you to completely take over the creation of a proper slug, which gets pretty involved. However, the process that removes the apostrophe is filtered so that plugin devs are able to add additional sanitation. You can add a preemptive callback that replaces apostrophes with hyphens before the usual sanitation function gets a chance to strip it out. Hook the “sanitize_title” filter with a priority argument less than 10 to do so.

    Thread Starter marcorroma

    (@marcorroma)

    Would you have a draft code to take as an example? I don’t know where to start…

    Moderator bcworkz

    (@bcworkz)

    Something like this (in functions.php, untested ):

    add_action('sanitize_title', function( $title, $raw, $context ){
       if ('save' == $context ) {
          $title = str_replace("'", '-', $title );
       }
       return $title;
    }, 5, 3 );

    A further improvement would be to only alter apostrophes bounded by letters and not single quotes before or after a space or other punctuation. Alter “L’alba” but not “For example ‘foobar’.”

    Thread Starter marcorroma

    (@marcorroma)

    Many thanks. It works perfectly!

    Moderator bcworkz

    (@bcworkz)

    Cool! You’re welcome.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Permalinks Fix’ is closed to new replies.