0

I'm writing a rewrite rule, accompanied by two additional rewrite tags, which (the tags) must apply only on a specific page. In the past, when I had to do something similar, I had done it like this:

add_action('init', 'publications_rewrite_rule', 10, 0);
function publications_rewrite_rule()
{
    if (preg_match('/^\/publications\//', $_SERVER['REQUEST_URI'])) {
        add_rewrite_tag('%date%', '([^&]+)');
        add_rewrite_tag('%publication%', '([^&]+)');
    }

    add_rewrite_rule('publications/([0-9]{4})/([0-9]{2})/([0-9]{2})/?$', 'index.php?page_id=370577&date=$matches[1]$matches[2]$matches[3]', 'top');
    add_rewrite_rule('publications/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^/]+)/?$', 'index.php?page_id=370577&date=$matches[1]$matches[2]$matches[3]&publication=$matches[4]', 'top');
}

In my current project though, WP installation is in a folder, ie. mydomain.tld/wordpress/, so preg_match() won't match the beginning of $_SERVER['REQUEST_URI'] to the page's slug ^\/quiz\/. I know I can hardcode WP's installation folder in the pattern, ie. ^\/wordpress\/quiz\/ but I'd like to get it programmatically, so that it works in future projects as well...

So how do I get the whole path of the active WP installation? If it's not one folder, like /wordpress/, I want to get the whole path, like /folder1/folder2/, etc, so that I can prepend it to my search pattern and do it the right/safe way...

1 Answer 1

1

You don't need to do this. As long as your site URL in Settings is set to mydomain.tld/wordpress you don't need to include /wordpress in your rewrite rules.

If you look at the rewrite_rules option in the wp_options table you'll see that none of the default rules are using it, because it's not required.

1
  • Yeah, you're right! How careless of me not noticing that earlier!
    – Faye D.
    Jun 10, 2022 at 4:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.