• OK, so this is a bit of a different one but let’s see if anyone can think of a simple solution …

    What we want is to be able to set a global permalink structure of, for example …

    /%variable_one%/%variable_two%/%postname%/

    … so that the base URL of the entire site is …

    example.com/%variable_one%/%variable_two%/

    Essentially I want WordPress to expect to be found two directory levels deep which can be anything and will be used on a custom basis by a theme / plugin.

    1) The current variable_one and variable_two values need to be able to be received by, for example $wp_query->query_vars[‘variable_one’]
    2) Permalinks to be displayed on the current page need to be able to have these variables set depending on what type of link is being written and other criteria

    We have managed to use this …

    function my_rewrite_tags() {
    add_rewrite_tag( ‘%variable_one%’, ‘([^&]+)’ );
    add_rewrite_tag( ‘%variable_two%’, ‘([^&]+)’ );
    }
    add_action(‘init’, ‘my_rewrite_tags’, 10, 0);

    … but cannot work out how to get WordPress to write future permalinks with what we want in (2) above.

    Any ideas?

    Thanks,

    Oliver

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

    (@bcworkz)

    The permalinks WP outputs are all filterable. Which filter depends on the post type. “post_link” for posts, “page_link” for pages, and “post_type_link” for other post types. If you added permastructs (with add_permastruct()) that use your rewrite tags, the tags will be in the URL passed to your filter callback. You would then substitute the required values for each tag. Even without permastructs, you can alter the URLs returned in any way that’s needed.

    Thread Starter Oliver Campion

    (@domainsupport)

    So each needs to be filtered separately. If that is so, how is it that when the option in Settings-Permalinks is changed to, for example …

    /test1/test2/%postname%/

    … this puts /test1/test2/ before all permalinks (posts, pages, archives, tags, categories)?

    Either way it looks like this is possible, so thank you.

    Does anyone know of any examples I can look into?

    Thank you.

    Moderator bcworkz

    (@bcworkz)

    Because that permastruct comes from the options table. It is retrieved before program flow splits off to various filters based on post type. By default none of those filters do anything, they are just available for custom coding.

    The example on this page might help. It’s for a custom taxonomy term, but the concept of filtering post_link is the same. The author uses a class declaration to accomplish this, but procedural methods can be used as well.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom Wildcard Permalink Structure’ is closed to new replies.