I’ve created a similar plugin for a client which can be seen in action here. It uses the following code to generate custom archive pages:
<?php
add_action('init', 'events_flush_rules');
add_action('generate_rewrite_rules', 'events_add_rewrite_rules');
add_filter('query_vars', 'events_query_vars');
add_action('parse_query', 'events_parse_query');
//Flush rules so WP will recalculate rewrite rules
function events_flush_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
/* Add your custom rules in the array below...the first part (the key,
to left of =>) is the regular expression to match, the second part
is the new value
*/
function events_add_rewrite_rules( $wp_rewrite )
{
/* Unsurprisingly by its name, this array contains your new rule.
The array uses your Regular Express (the express tests the raw
URL for matches) as the key. The value (the string to the right
of the "=>" is the new URL. In the function below, you will add
your parameter names to $public_query_vars[].
Separate Key + Value pairs with a comma. Do not put a comma after
the last pair...that always gets me b/c I copy/paste a lot.
Change the page (regions) to your page name, change/add/remove
variables and their corresponding regex matches "(.+)" to match
your needs
*/
$new_rules = array(
'events/([0-9]{4})/([0-9]{1,2})/?$' => 'index.php?year='.$wp_rewrite->preg_index(1).'&m='.$wp_rewrite->preg_index(2).'&event=1'
);
//Add the rules to the rules array..wanna add them to the TOP, like so
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
function events_query_vars($public_query_vars) {
/* ADD YOUR PARAMETERS or QUERY VARIABLES BELOW
Uncomment the lines and change the variable names to the ones
you want to use. Add more lines as needed
*/
$public_query_vars[] = "event";
/* Note: you do not want to add a variable multiple times. As in
the example above, multiple rules can use the same variables
*/
return $public_query_vars;
}
function events_parse_query($n){
if(get_query_var('event') && get_query_var('m')<=12 && $year=get_query_var('year')){
$m = get_query_var('m');
query_events('showevents=100&m='.$m.'&year='.$year);
if(have_events()){ //custom function for my plugin
$template = TEMPLATEPATH . "/events.php";
include($template);
exit;
}
}
}
In the theme directory I have created a template called events.php which has the code to display events happening in that month (a bit like archives.php).
have_events()
and query_events()
are functions in my plugin which query the database for events similar to the wordpress loop.