[wp-hackers] Customising date based URLs to use a 2 digit year

Mark Duncan wp-t31os at ntlworld.com
Mon Apr 26 16:14:39 UTC 2010


http://codex.wordpress.org/Function_Reference/WP_Rewrite#Plugin_Hooks

This would rewrite the necessary date patterns in the rewrite rules.

add_filter( 'date_rewrite_rules' , 'two_digit_year_rules' , 10 , 1 );

function two_digit_year_rules( $rules ) {
	$new_rules = array();
	foreach( $rules as $pattern => $query_str ) {
		$pattern = str_replace( '([0-9]{4})' , '([0-9]{2})' , $pattern );
		$new_rules[ $pattern ] = $query_str;
	}
	unset( $rules );
	return $new_rules;
}
That will not however rwrite the various archive links on your site that 
still return a 4 digit year or, year based queries that will still 
expect a 4 digit year..

WP_Query / query_posts query vars will be expecting a 4 digit value, 
when you feed it two, it won't find any posts.

You'll probably need to intercept the query vars and check when a year 
query var is present, fill in the missing "20" at the start of the year, 
and return the new value, simply so the queries receive a valid year.

The above filter will do the rule / pattern swapping for you, you'll 
just need to work out the query vars issue.

I've only tested the filter briefly, but i can see there's some deeper 
dependancies elsewhere that need to be covered to, else you'll end up 
with empty query sets and invalid 4 digit year based links.

Hope that helps anyway... ;)

Mark

Christian Foster wrote:
> Yes, that was the initial thought but he would like to keep them in
> the same format - having looked through all the rewrite rules I think
> it is possible by replacing the 4 digit year to 2, and adding 20 to
> the front of the year query variable. He doesn't have anything before
> 2000.
> 
> On Mon, Apr 26, 2010 at 12:31 AM, Mike Schinkel
> <mikeschinkel at newclarity.net> wrote:
>> Have you considered doing 301 redirects from the old format to the new format?  If viable you can do that in the .htaccess file or using the "template_redirect" hook by sniffing $_SERVER['REQUEST_URI'].
>>
>> -Mike
>>
>> On Apr 25, 2010, at 8:55 AM, Christian Foster wrote:
>>
>>> Hi all,
>>>
>>> I have been a bit stumped by this and can't find a solution anywhere -
>>> a potential client wants to move from Moveable Type to Wordpress. His
>>> MT urls are in the format http://site.com/YY/MM/DD and he posts one
>>> post per day (always!).
>>>
>>> Does anyone know if there a way for Wordpress to
>>> a) use a 2 digit year instead of 4 and
>>> b) get wordpress to pick up the post for that date on the single day
>>> URL instead of showing the single day archive (My thoughts are to
>>> write a custom template for the daily archive and change the query to
>>> pick up the 1 post for that day)?
>>> c) remove the normal wordpress URL for the individual posts? My
>>> initial thoughts are to filter the permalinks to point to the date of
>>> the post.
>>>
>>> Regards,
>>>
>>> Christian.
>>> _______________________________________________
>>> wp-hackers mailing list
>>> wp-hackers at lists.automattic.com
>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
> 
> __________ Information from ESET NOD32 Antivirus, version of virus signature database 5062 (20100426) __________
> 
> The message was checked by ESET NOD32 Antivirus.
> 
> http://www.eset.com
> 
> 
> 
> 



__________ Information from ESET NOD32 Antivirus, version of virus signature database 5062 (20100426) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com




More information about the wp-hackers mailing list