Skip to content

Instantly share code, notes, and snippets.

@bkeating
Last active January 1, 2016 18:59
Show Gist options
  • Save bkeating/8187515 to your computer and use it in GitHub Desktop.
Save bkeating/8187515 to your computer and use it in GitHub Desktop.
Simple HOWTO to get 5 digit year support in WordPress.

5 Digit Year support in WordPress

This is a cosmetic tweak only, meaning WordPress, your database and system in general will not recognize the 5 digit year as a valid year. This is fine however. With this tweak we get it where it counts; Your URLS will cite 5 digit years and your template can be adjusted to also display them. It displays where your users will see it and that will get them thinking.

  1. Go to your WordPress admin panel. Under Settings -> Permalinks select Custom Structure and add your padded zero:

     /0%year%/%monthnum%/%day%/%postname%/
    
  2. Since your URL structure just changed, add rewrites for URLs citing 4 digit years (legacy support). Under Nginx add:

     rewrite "^/([0-9]{4})/$" /0$1/ permanent;
     rewrite "^/([0-9]{4})/([0-9]{2})/$" /0$1/$2/ permanent;
     rewrite "^/([0-9]{4})/([0-9]{2})/([0-9]{2})/$" /0$1/$2/$3/ permanent;
     rewrite "^/([0-9]{4})/([0-9]{2})/([0-9]{2})/([\w-]+)/$" /0$1/$2/$3/$4/ permanent;
    
  3. Now URLS and links will cite the padded zero but your page templates will still display 4 digit years. Assuming you use jQuery, you can drop this snippet in your template footer to get that archive list (if you display one) to show 5 digit years.

     <script>
         $(document).ready(function() {
             // Find the archive list and prefix years with a zero.
             if ($('.archive_list')) {
                 var n = $(".archive_list li").length;
                 $('.archive_list li a').each(
                     function(n, element){
                         year = $(element).text();
                         long_year = ("0" + year);
                         $(element).text(long_year);
                     }
                 );
             }
         });
     </script>
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment