url rewriting – Get wordpress installation folder


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…

Faye D. 2 months 2022-06-09T16:58:08-05:00 0 Answers 0 views 0

Leave an answer