Close
Menu

RTCXpression

Permalinks with File Name Extensions in WordPress


June 26, 2017

Anyone who’s ever used WordPress for a while knows about the shortcomings with the permalinks. I’ve used it since 2006, pulling my hair out along the way. The only real changes to the structures available have been improvements to the underlying code, not the options.

The structures available, using only the supported tags, will work for all posts, pages and taxonomies. Problems appear when you try to do something different, like add file name extensions.

Adding a File Name Extension to Permalinks

I’m using the “.html” file name extension here. You can use any file name extension web browsers will recognize as HTML. The code I’m presenting is how I have things set up. Feel free to change it to suit your needs.

For posts and posts only, you can go into the permalink settings, select the custom structure and tack on “.html”. A post is the only type of page it affects. Other pages will simply ignore it (no slash or anything after the slug). Here’s my example:

/%postname%.html

So how do we do it with the rest of the pages, like author, category and tag pages? I went through tons of code trying to find the best way to do it and this is what I came up with:

function html_rewrite($rules) {
  foreach ($rules as $key => $value) {
    $newrules[str_replace('/?', '.html', $key)] = $value;
  }
  return $newrules;
}
function html_link($link) {
  return $link . '.html';
}
add_filter('author_rewrite_rules', 'html_rewrite', 3);
add_filter('author_link', 'html_link', 1);
add_filter('category_rewrite_rules', 'html_rewrite', 3);
add_filter('category_link', 'html_link', 1);
add_filter('page_rewrite_rules', 'html_rewrite', 3);
add_filter('page_link', 'html_link', 1);
add_filter('tag_rewrite_rules', 'html_rewrite', 3);
add_filter('tag_link', 'html_link', 1);

I don’t use any other types of pages. Now, if we want to change the base names of the permalinks, it gets trickier. You know, the permalinks with “/author/”, “/category/’ and ‘/tag/’ within them?


Changing the Base Names and Adding File Name Extensions

If we’re going to change the base names, we might as well keep from doing things twice. The changes to the base names are examples. I still use the original base names.

/* Add file name extension only to standard pages */

function html_rewrite($rules) {
  foreach ($rules as $key => $value) {
    $newrules[str_replace('/?', '.html', $key)] = $value;
  }
  return $newrules;
}
function html_link($link) {
  return $link . '.html';
}
add_filter('page_rewrite_rules', 'html_rewrite', 3);
add_filter('page_link', 'html_link', 1);

/* Change base names AND add file name extensions to other pages */

function change_permalinks() {
  global $wp_rewrite;
  $wp_rewrite->author_base = 'writer';
  $wp_rewrite->author_structure = '/' . $wp_rewrite->author_base . '/%author%.html';
  add_permastruct('category', 'topic/%category%.html');
  add_permastruct('post_tag', 'label/%post_tag%.html');
}

As you can see, we can add “.html” while changing the base names (for other than standard pages). With my static website, I changed the slash preceding the first percentage symbol to a dash (or hyphen).

There was a long period when I didn’t use author or tag pages. Mastering this code allowed me to do so without writing a lot of workarounds.

WordPress Complexity and Making Changes

WordPress is a complex piece of software, with well over a decade of code changes. The reason we have WordPress developers today is because that, along with backwards compatibility, makes it almost impossible for the average person to make changes the way they’re supposed to be made.

This thing with permalinks should have been taken care of long ago. One of the WordPress mantras is “decisions, not options”. What that really means is that WordPress administrators have to be programmers or hire developers to do the programming for them.

In my opinion, and possibly only my opinion, every change should be controlled by options in the user interface. If nothing else, permalinks should be controlled 100 percent by options, for all page types.

Please go to this alternate page if you would like read or post comments.

Share:

Categories: Technology

Tags: , , , ,

Previous and Next Articles (if any):

« »

More

Please read some of my more important pages if you have the time:

Comments Policy           Privacy Policy

RTCXpression established Feb 28, 2011
Copyright © 2013-2017 RT Cunningham
Hosted at Digital Ocean