Make WordPress Core

Ticket #11931: rewrite-day-month-year.php

File rewrite-day-month-year.php, 1.7 KB (added by Frenzie, 13 years ago)
Line 
1<?php
2/*
3Plugin Name: Rewrite Rules for Day/Month/Year
4Plugin URI: http://frans.lowter.us/2010/01/22/teaching-wordpress-some-manners-enabling-daymonthyear-archives
5Description: Wordpress can't cope with /%day%/%monthnum%/%year%/ for some reason. That is to day, it fails when you try to go for an archive in the form of /date/month/year/ This teaches it some manners. Probably/hopefully shouldn't interfere with other structures, but why you'd activate it if you don't need it I wouldn't know.
6Version: 1.0
7License: GPL
8Author: Frans
9Author URI: http://frans.lowter.us
10*/ 
11
12function test_add_rewrite_rules( $wp_rewrite ) {
13        $new_rules = array(
14                "date/(\d{2})/(\d{4})" => 'index.php?m=' . $wp_rewrite->preg_index(2) . $wp_rewrite->preg_index(1),
15                "date/(\d{4})" => 'index.php?year=' . $wp_rewrite->preg_index(1)
16        );
17        $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; //NOTE: You must add it to the start of the array, Else WP's greedy rules at the end of the array will eat the request
18}
19
20register_activation_hook( __FILE__, 'flush_rules_initiate' );
21register_deactivation_hook( __FILE__, 'test_flush_rules' );
22// add_action('init','test_flush_rules'); // for testing
23
24function flush_rules_initiate() {
25        // Add the permalink override stuff
26        add_action('generate_rewrite_rules', 'test_add_rewrite_rules');
27        test_flush_rules();
28}
29
30function test_flush_rules(){
31        //Flush the rewrite rules so that the new rules from this plugin get added,
32        //This should only be done when the rewrite rules are changing, Ie. When this plugin is activated(Or Deactivated), For simplicity while developing using WP Rewrite, I flush the rules on every page load
33        global $wp_rewrite;
34        $wp_rewrite->flush_rules();
35}
36?>