I am trying to add a wp rewrite for my plugin that allows users to include a shortcode on a page that will then display a calendar. This calendar then requires url params in order to navigate the dates. All this is working fine without issues...it is now getting it to work if a user includes the shortcode on a page that has a parent page or pages. So another words if the page with the shortcode is on is called 'class-calendar' and the user puts it as a subpage to a parent page called 'yoga' then the url may look like mysite.com/yoga/class-calendar/ and if you navigate the schedule then it will look like mysite.com/yoga/class-calendar/7/4/2014. I have it working fine if it is like mysite.com/class-calendar/7/4/2014 but I need to able to make so it works for an unknown parent page or pages. I can make it work like the code sample below $newrules = array();

        $newrules['([^/]*)/([^/]*)/' . $this->schedule_page_slug . '/([^/]*)/([^/]*)/([^/]*)/?$'] = 'index.php?pagename=$matches[1]/$matches[2]/' . $this->schedule_page_slug . '&month=$matches[3]&days=$matches[4]&years=$matches[5]';

        $newrules['([^/]*)/' . $this->schedule_page_slug . '/([^/]*)/([^/]*)/([^/]*)/?$'] = 'index.php?pagename=$matches[1]/' . $this->schedule_page_slug . '&month=$matches[2]&days=$matches[3]&years=$matches[4]';

        $newrules[$this->schedule_page_slug . '/([^/]*)/([^/]*)/([^/]*)/?$'] = 'index.php?pagename=' . $this->schedule_page_slug . '&month=$matches[1]&days=$matches[2]&years=$matches[3]';

        return $newrules + $rules;

However, even though this works I know it is not right and it only handles the page with the shortcode to be 2 pages deep ( not that it really should go any deeper, but just the same I know how I have it is not right)

Any help getting me pointed in the right direction would be much appreciated. I have been trying to mess with a wild card parent, but can't get it to work. Thanks

2 Answers 2


You can use the pattern (([^/]*)/)? to match a full <component>/ construction - this will create a multi-dimensional element in the $matches array, with the first level containing the full expression and the internal matches containing an item with <component> without the '/' character. However, you need to be very careful in the way you handle the results, as it may show some unexpected behavior since you are basically trying to "merge" two components into one matched element - for example, if the entire element doesn't exist (both components), all the subsequent matches will be moved with one position in the $matches array.

For more references on how the $matches array is created, study PHP's preg_match_all method, as well as some good tutorials on regular expressions to figure out how those rewrite rules are built.

  • Can you give me an example?...I am a little confused with your explanation. Nov 9, 2014 at 20:52

Another way you could solve this is to instead add a rewrite endpoint, which would allow it to work anywhere at any page depth and/or post type. The only drawback is that you will have to parse out the dates from the single enpoint query var with php rather than having the individual query vars set for you, but this should be a simple enough task by splitting the endpoint value on the slash with php's explode.

function wpd_calendar_endpoint(){
    add_rewrite_endpoint( $this->schedule_page_slug, EP_PERMALINK | EP_PAGES );
add_action('init', 'wpd_calendar_endpoint');

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.