• Kaelri

    (@kaelri)


    Hi! I want to customize my post/page permalinks in a certain way, and I’m trying to figure out the best approach. I’ve searched this forum and found a lot of ideas that are similar, but not quite what I want to accomplish.

    Pages

    Basically, I want to create an optional custom field on single posts. 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:

    • Book 1
    • Chapter 1
    • Book 2
    • Chapter 1

    I want the permalinks to look like this:

    • /book1
    • /book1/chapter1
    • /book2
    • /book2/chapter1

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

    Parents

    In addition—and this might be more complicated—I’d like this logic to be applied to each part of a child-page permalink independently. 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

    Etc. This is why I don’t want to use the Custom Permalinks plugin, which stores the entire custom permalink as a single string. So I can’t change the parent page without subsequently updating all the children manually. (I suppose I could add a filter that updates cf_custom_permalink in a certain way, but ideally I’d like to avoid using plugins at all.)

    Obviously, there would also have to be some kind of error handling to make sure URLs are never duplicated.

    I’m kind of surprised I haven’t seen this before, which makes me suspect it’s not possible (or very difficult), but any guidance would be appreciated. I’m comfortable writing PHP and editing theme code, so if it’s a job for filters, I’m game.

  • The topic ‘Custom Field for Singular Permalinks’ is closed to new replies.