• Resolved Pamela

    (@infoknutsford-softwarecouk)


    I want the permalink to be town then 2 dashes then the phone number. Eg

    town–9999-111-111

    Is it possible to have — and if so how? It gets replaced with –

    I tried adding

    add_filter( 'run_wptexturize', '__return_false' );
    add_filter( 'sanitize_title', function( $title )
    {
        return str_replace( '--', 'twotempdashes', $title );
    },9 );
    
    add_filter( 'sanitize_title', function( $title )
    {
        return str_replace( 'twotempdashes', '--', $title );
    },11 );

    To functions.php but it didn’t make any difference

Viewing 9 replies - 1 through 9 (of 9 total)
  • Are you referring to the slug of a post?
    If so, the slug is not run through the sanitize_title filter like that.

    And the run_wptexturize filter page says “The filter runs only once, the first time wptexturize() is called.”

    Looking at your example, it’s difficult to tell that there are two dashes at the beginning. It seems like an odd thing to try to do.
    You might want to read this code:
    https://developer.wordpress.org/reference/functions/wp_unique_post_slug/

    Moderator bcworkz

    (@bcworkz)

    There are two sides to permalinks, the output to the browser, and the WP interpretation of requested permalinks. These need to be coordinated for success. Additionally, whatever code establishes the output permalink needs to be able to consistently build the link from available data. If the data is unavailable or inconsistent, the code cannot do what is expected of it.

    Where does the permalink you want to alter appear on your site? Whatever function causes this output is what needs to be altered. For example, if the output is due to the_permalink(), we use the filter “the_permalink” to alter output.

    Currently, is WP properly returning the desired content with your desired permalinks? Or does this side of the issue also need to be addressed?

    Thread Starter Pamela

    (@infoknutsford-softwarecouk)

    I am trying to match what an old version of the site had due to SEO. The links in the address bar where town–9999-111-111. Someone has created a new version of the site in WP and it doesn’t like –. It replaces them with –

    Thread Starter Pamela

    (@infoknutsford-softwarecouk)

    Thread Starter Pamela

    (@infoknutsford-softwarecouk)

    I think what I need to is to replace sanitize_title_with_dashes changing $title = preg_replace(‘|-+|’, ‘-‘, $title); to $title = preg_replace(‘|+|’, ‘-‘, $title);

    How do I do that? I know how to add the replacement but how do I remove the original? I don’t know what to put for $tag.

    Thanks

    Moderator bcworkz

    (@bcworkz)

    Unfortunately, sanitize_title_with_dashes() does not have any filters available for altering what it does. Apparently neither do any of the functions that call this function. Thus this is not the way to correctly do what you want.

    Is the entire issue that WP is converting double hyphens to singles? If it allowed doubles, all would be perfect? And you need the double hyphen only because you want to retain accumulated page rank from the old double hyphen URLs? My advice is to add an .htaccess redirect rule that 301 redirects double hyphen requests to the equivalent single hyphen URL. All major search engines should transfer accumulated page rank to the new URL when such a redirect is encountered.

    Thread Starter Pamela

    (@infoknutsford-softwarecouk)

    Sorry I should have said. I changed the functions.php file and removed the sanitize_title_with_dashes() filter then added my version back without the line about hyphens and it is working perfectly

    Moderator bcworkz

    (@bcworkz)

    Ah! I see now, sanitize_title_with_dashes() is a filter callback which you replaced with your own version. Nicely done!

    Thread Starter Pamela

    (@infoknutsford-softwarecouk)

    One of my sudden inspirations 🙂

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘permalinks’ is closed to new replies.