• Resolved Kaelri

    (@kaelri)


    Hi! I’m trying to customize my page permalinks in a specific way. Basically, I want to create an optional custom field on single pages. If this field has a value, WordPress should use it in place of the post slug when generating permalinks.

    Say I have a page structure like this:

    • Chicago
    • Population
    • Landmarks
    • Baltimore
    • Population
    • Landmarks

    I want the permalinks to look like this:

    • /chicago/
    • /chicago/population/
    • /chicago/landmarks/
    • /baltimore/
    • /baltimore/population/
    • /baltimore/landmarks/

    Normally, since WordPress requires unique post slugs (which I don’t want to change), those two “Population” pages cannot both have “/population/” as the URL part. So I want to add a new field, set the value to “population” for both pages, and have WordPress pick it up and use it.

    The same logic should apply to parent pages. For example, given the pages:

    • Parent (slug: ‘parent-slug’, custom field: ‘parent-custom’)
    • Child (slug: ‘child-slug’, custom field: ‘child-custom’)

    If only the parent has the custom field, then the permalink should be: /parent-custom/child-slug

    If only the child has the custom field, then the permalink should be: /parent-slug/child-custom

    And if they both are customized, then: /parent-custom/child-custom

    Obviously, there would also have to be some kind of error handling to make sure URLs are never duplicated. Maybe some function that is triggered by the save_post hook? I’m comfortable writing PHP and editing theme code, so if it’s a job for filters, I’m game.

    (Note to mods: I posted a similar question about six months ago, but I realized it was in the wrong forum, so I’m reposting here.)

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Your assumption of no duplicate slugs is flawed. What version of WP do you have? I forget when duplicate hierarchical slugs became permissible as long as the parents were different. It’s been a while anyway. If you really can’t do this, it’s time to update. Despite it technically being allowed in all recent versions, it can be tricky to implement in practice. You must select the parent page before entering the title so WP knows it’s OK to allow a duplicate slug because the parents are different.

    The problem comes about when one forgets to pick the parent first. Then WP assigns a unique slug by appending a number. It takes some maneuvering to recover from this and stop WP from appending numbers even though the parents are different. I’m not sure exactly what’s involved through the usual user interface. It’s possible, but hardly intuitive, so you need a better way to get WP to “forget” to append numbers. The ‘wp_unique_post_slug’ filter can be used to force a duplicate slug, but your callback does need to insure this is OK by confirming there is no parent that’s the same as the one selected for any existing pages that have the same original, un-appended slug.

    Despite this tweak, one still must always select a parent before entering a title. I don’t see any easy way around this, the practice just needs to be incorporated into anyone’s workflow who creates such pages. IMO a small price to pay for not needing to deal with custom fields and playing filter and rewrite games second guessing what WP does.

    Thread Starter Kaelri

    (@kaelri)

    Well, I’m delighted to be wrong. I’m using the current version of WordPress, I just had no idea this had been addressed.

    In fact (now that I’ve done some testing), I don’t even have to pick the parent first. When I create a duplicate page, it generates a number-appended slug as usual. But then I can move it under a parent, edit the slug to remove the number, and it’s fine. Zero effort.

    So, uh, problem solved. Thanks for disabusing me of my antiquated WordPress dogma. ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Field for Singular Permalinks’ is closed to new replies.