WordPress.org

Ready to get started?Download WordPress

Ideas

sanitize_title_with_dashes() should replace "&" with "and" instead of stripp

  1. davejuk
    Member

    12345

    When I create a new post/page, the permalink that WordPress generates from the title (using sanitize_title_with_dashes()) has any & characters removed so I have to manually enter the word "and". It would be nice if it did this automatically.

    Posted: 1 year ago #
  2. Justin Tadlock
    Member

    12345

    This is probably not such a good idea. & does not always equate to "and" in this case. & can be used any number of ways. We shouldn't start guessing how the user intends it to be used.

    The one thing we do know is that & shouldn't be used in the URL.

    Posted: 1 year ago #
  3. davejuk
    Member

    12345

    In what possible blog post title could " & " mean anything other than " and "?

    Even if there are some obscure examples, it will mean "and" in more than 99.9% of post titles and the slug will have more meaning with it included.

    Posted: 1 year ago #
  4. Justin Tadlock
    Member

    12345

    — is just one of many.

    Can you provide the source for your statistics?

    Posted: 1 year ago #
  5. davejuk
    Member

    12345

    The & in your example is not folled by a space so it would not be processed in the way I am describing.

    Again, in what possible blog post title could " & " mean anything other than " and "?

    With regard to statistics, I am obviously making a generalisation but if you search for & on Google Blog Search I am confident that you will get to the 1,000th result before you find a title where " & " means anything other than " and ".

    Posted: 1 year ago #
  6. Justin Tadlock
    Member

    12345

    I just showed you one example. I'm not going to continue listing the many others because it's irrelevant.

    You have not described any "process" by which this change in the function should occur other than that you'd like it to be changed.

    Nevertheless, the sanitize_title_with_dashes() function cannot change to suit your request. Doing so would break backwards compatibility in WordPress as well as theme/plugins that use this function.

    Posted: 1 year ago #
  7. davejuk
    Member

    12345

    Your example is invalid for the reason I explained in my last post. To clarify, your example string does not contain the string " & " so no replace would occurr.

    I explained my idea well enough for you to understand it in your first two replies so I don't know what you don't understand now.

    If changing sanitize_title_with_dashes() would break older plugins/themes then the most obvious solution would be to add an optional parameter or setting. I'm sure this has been done before.

    Posted: 1 year ago #
  8. Justin Tadlock
    Member

    12345

    You're limiting the use of "&" to the English language. If you want to continue with your current argument, at the very least, you should be arguing for "&" to be changed to "et".

    In your current scenario, anyone who doesn't blog in English and wants to use an ampersand in their titles would need to install a plugin to change the (formerly working) functionality of WordPress. This is not really something we could internationalize because it'd need to be used in URLs. It's something that needs to be hardcoded.

    A better idea would be for you to develop a plugin that changes this character for post titles that others can also download and install. It should be simple enough.

    Your example is invalid for the reason I explained in my last post. To clarify, your example string does not contain the string " & " so no replace would occurr.

    That was not made clear in your previous post. I concede that the string " & " could be changed without harm to character entities.

    I explained my idea well enough for you to understand it in your first two replies so I don't know what you don't understand now.

    Where did I say I didn't understand what you wrote?

    If changing sanitize_title_with_dashes() would break older plugins/themes then the most obvious solution would be to add an optional parameter or setting.

    This might be a possibility for the function, given that no other issues existed. I'd have to dig more into the code to tell you whether it's a viable option.

    Posted: 1 year ago #
  9. Justin Tadlock
    Member

    12345

    Here's a fully-functioning plugin that'll do the job for you.

    <?php
    /**
     * Plugin Name: Ampersands In Titles
     * Plugin URI: http://justintadlock.com
     * Description: Replaces ampersands with the text "and" when the 'sanitize_title' hook is called.
     * Version: 0.1
     * Author: Justin Tadlock
     * Author URI: http://justintadlock.com
    */
    
    add_filter( 'sanitize_title', 'my_sanitize_title', 9 );
    
    function my_sanitize_title( $title ) {
    	return str_replace( " & ", " and ", $title );
    }
    
    ?>
    Posted: 1 year ago #
  10. davejuk
    Member

    12345

    Wow - thank you!

    I hadn't considered how other languages might be affected.

    I would still argue that because of how common the & character is in post titles, it should get some special treatment when it comes to creating slugs. An obvious solution would be to make the replacement string configurable from within Settings. I have never written a plugin before so it could be a simple first project.

    Posted: 1 year ago #

RSS feed for this topic

Reply »

You must log in to post.

  • Rating

    12345
    2 Votes
  • Status

    This is plugin territory

No tags yet.