Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My WordPress website currently uses this custom permalink structure: %author%/%year%/%monthnum%/%day%/%postname%

In this case, %monthnum% instantiates numbered dates with a leading zero -- e.g., "09" for September. So, a sample URL might look like this:
mywebsite.com/username/2012/09/12/post-name

Is there a function I can add or .htaccess change that I can make which will remove the leading zeros from my permalink stucture? So, using the example above, my URLs would ideally look like this:
mywebsite.com/username/2012/9/12/post-name

Thank you! I've read up on WordPress' structure tags documentation (http://codex.wordpress.org/Using_Permalinks), but I can't find any solutions or plugins for the above-mentioned problem.

share|improve this question
add comment

1 Answer

Filter 'month_link' and 'day_link', WordPress will find the matching posts then without further work.

Sample code:

add_filter( 'month_link', 't5_strip_leading_zeros_in_url' );
add_filter( 'day_link',   't5_strip_leading_zeros_in_url' );

function t5_strip_leading_zeros_in_url( $url )
{
    // no pretty permalinks
    if ( ! $GLOBALS['wp_rewrite']->get_month_permastruct() )
    {
        return $url;
    }

    return str_replace( '/0', '/', $url );
}
share|improve this answer
    
Sorry, it is not obvious to me: What file should this code be placed in? Thank you! –  user20783 Sep 24 '12 at 16:54
    
@DinaDadian This should go into a new plugin. –  toscho Sep 24 '12 at 16:55
    
@toscho Thank you! I tried using this code in a new plugin (my plugin activated just fine), but I'm not seeing a change in my permalinks structure. Also tried adding the code to my functions.php file, but this didn't work either. Sorry! Any ideas? In case this matters, one more detail -- my website is built on top of BuddyPress 1.6. –  user20567 Sep 26 '12 at 19:30
    
Ah, someone helped me pinpoint the issue here. The solution above applies to month and day links. In order to have it apply to the entire post link, add this to the beginning of @toscho's code: add_filter( 'the_permalink', 't5_strip_leading_zeros_in_url' ); Works great for me. Thanks for your help! –  user20567 Oct 3 '12 at 1:39
    
Tick the check mark (✔) below the vote buttons if my answer solved your problem. :) –  toscho Oct 3 '12 at 1:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.