$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Removing leading zeros from custom permalink structure|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Removing leading zeros from custom permalink structure

matteradmin9PV0评论

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/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/username/2012/9/12/post-name

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

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/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/username/2012/9/12/post-name

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

Share Improve this question asked Sep 18, 2012 at 19:19 user20567user20567 812 bronze badges 1
  • Added code to htaccess, still it didn't work, moreover it broke all my images. – dh47 Commented Mar 6, 2019 at 7:34
Add a comment  | 

3 Answers 3

Reset to default 8 +50

How about using custom rewrite tags/structure?

So we'll be using these two rewrite/structure tags:

  1. %monthnum2% does the same thing as the %monthnum% tag, but without a leading zero; e.g. 3 and not 03 for March.

  2. %day2% does the same thing as the %day% tag, but without a leading zero; e.g. 7 and not 07.

The steps:

  1. In the theme functions file (functions.php), add:

    add_action( 'init', function(){
        add_rewrite_tag( '%monthnum2%', '([0-9]{1,2})', 'monthnum=' );
        add_rewrite_tag( '%day2%',      '([0-9]{1,2})', 'day=' );
    } );
    

    That will generate the %monthnum2% and %day2% (rewrite) tags and be used when WordPress (re-)generates the rewrite rules.

    And then add this:

    add_filter( 'post_link', function( $permalink, $post ){
        if ( preg_match( '/%(?:monthnum2|day2)%/', $permalink ) ) {
            $time = strtotime( $post->post_date );
            $date = explode( ' ', date( 'n j', $time ) );
    
            $permalink = str_replace( [
                '%monthnum2%',
                '%day2%',
            ], [
                $date[0],
                $date[1],
            ], $permalink );
    
            $permalink = user_trailingslashit( $permalink, 'single' );
        }
        return $permalink;
    }, 10, 2 );
    

    That will replace the rewrite tags in the permalink.

  2. Go to the permalink settings page and then in the "Custom Structure" box, enter this structure: /%year%/%monthnum2%/%day2%/%postname%/ or /%author%/%year%/%monthnum2%/%day2%/%postname%, whichever applies.

    The point is, use %monthnum2% to display the month number without a leading zero and %day2% to display the day number without a leading zero.

  3. Save your changes — both the theme functions file and the permalink settings — and go to the "Posts" page (wp-admin/edit.php) and just check everything (mouse-over the post permalink and visit the post).

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 );
}

htaccess solution

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/.*?/.*?/[0][1-9]/.*?/.*?$
RewriteRule ^(.*?)/(.*?)/[0]([1-9])/ /$1/$2/$3/ [R=301]
</IfModule>

I created a working example for you to test here

What this does is check for the path structure provided if the third path contains, a leading 0 and number 1-9, eg /03/, then applies the rewrite rule to the third-path, leaving the first two paths and additional paths intact (.*?)/(.*?)/. The rule only changes the third path if that path matches (01-09) [0]([1-9]) from eg /03/ to /3/.

Does not affect /uploads/ month images path because the uploads structure does not match the structure provided. Please test using above link to see if it suits your needs.

Post a comment

comment list (0)

  1. No comments so far