1

I am trying to convert a post title to a slug. I used sanitize_title_with_dashes, thinking that's what WordPress uses. However, if my post title has an apostrophe in it, sanitize_title_with_dashes does not strip out the apostrophe. Instead, it escapes the apostrophe with a backslash.

For example, if I use the default post editor to create a post named "Bob's Boutique", WordPress core will correctly create the slug as "bobs-boutique".

If I try to convert "Bob's Boutqiue" to a post slug using sanitize_title_with_dashes in functions.php, the result is "bob\'s-boutique".

Is there an additional function that WordPress uses when making slugs that strips out apostrophes?

2
  • 1
    echo sanitize_title_with_dashes("Bob's Boutqiue") outputs bobs-boutique, I don't get an escaped quote.
    – Milo
    Commented Jan 22, 2016 at 17:14
  • sanitize_title_with_dashes should work, so we need to see more code to know why the value is being escaped - perhaps you are also wrapping it in a WordPress esc_html or another function.
    – Q Studio
    Commented Jan 10, 2021 at 8:59

1 Answer 1

1

Use this function to remove the apostrophe:

preg_replace( "/[:’]/", "", $title );
2
  • Really? Add it where exactly? Can we just drop that in functions.php and BAM! It works?
    – s_ha_dum
    Commented Jan 24, 2016 at 2:37
  • What @s_ha_dum means is that this is not gonna work.. the very least it requires is to inside a function, allowing the $title to be passed to it..
    – Q Studio
    Commented Jan 10, 2021 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.