WordPress.org

Ready to get started?Download WordPress

Forums

Permalink modification on wp_insert_post (4 posts)

  1. powermaniac
    Member
    Posted 7 months ago #

    Hello, I'm not sure if this is the right area... When adding a new post with wp_insert_post, all periods (.) in the url are replaced with dashes. This is usually fine for SEO, but in this particular case, I would like them to remain as periods. I have a site that needs to have other site URL's in my URL.

    For Example:

    mysite.com/domain/www.wordpress.org

    When I use wp_insert_post, it changes to:

    mysite.com/domain/www-wordpress-org

    Any ideas?

  2. bcworkz
    Member
    Posted 7 months ago #

    Even if you managed to get the periods to remain (not too hard), for some reason a post slug containing periods cannot be found by query (I tried). I'm not sure why, but it would probably take some deep core hacking to alter the behavior. I don't think it's worth pursuing.

    Just my opinion, feel free to continue if you like.

    Curiously, for a post with the slug www-wordpress-org, entering example.com/www.wordpress.org in your browser will retrieve the correct post! Address remains with periods in the browser, but of course the permalink on the page has hyphens. Maybe you can make use of that somehow?

  3. powermaniac
    Member
    Posted 7 months ago #

    Hi bcworkz,

    I was actually aware of url with periods working. The reason why I want this to be without dashes is more or less for SEO. I want to be able to pull my custom post type (which is not a problem), but I want the urls to point to the period version, not dash version. The SEO reason is that I want my URL to show up if searching for wordpress.org for example, as that URL is in one of mine.

    You mention that it's not too hard to get the periods to remain... What solution do you recommend? When you're referring to the slug showing incorrectly, are you referring to pulling the permalink when using a query? Regardless, I can give it a shot and see if your solution will work in my case. The only other solution I can think of is to pull the URLs with dashes through the query, and pull some string replacement to turn dashes to periods. My issue with this is that some URLs would cause an issue. For example, http://www.my-site.com would turn to http://www.my.site.com

  4. bcworkz
    Member
    Posted 7 months ago #

    The easy way to store URLs with periods that I had in mind would be to reinsert the periods during a save post action or similar, after the mechanism that changed to hyphens in the first place. You run into the same issue though of being unable to distinguish intentional hyphens from automatically inserted hyphens.

    Better would be to intervene with the actual slug creation routine. Much of this is javascript, but I believe an AJAX query is made, and the PHP handler uses wp_unique_post_slug() to return the periodless slug. I'm not 100% sure of this, I have limited resources right now to make a proper confirmation. It's something along this line though. There is a filter of the same name as the function that could be used to restore only those periods that were in the initial title string.

    The problem remains that the query cannot find a slug with periods. You might see if querying for a slug with hyphens will properly locate a post where the slug is actually periods, a reverse of the current situation. Somehow, I don't think the query will find it. You might also try escaping the periods before the query, this sounds a little more promising.

    About the "incorrect slug". It's not really incorrect. I meant in the situation where you enter a slug with periods, it will find the actual slug with hyphens, the current situation. Any permalinks will thus have hyphens, correctly so. But finding such a slug when the query string has periods is done unknown to the browser, so the browser continues to show a slug with periods. Everything on the WP side will be assuming hyphens are correct.

Reply

You must log in to post.

About this Topic