Menu

RTCXpression

Close

Add File Name Extensions to WordPress Pages, Category and Tag Pages

- May 5, 2017

file name extensions There are plenty of reasons to add file name extensions to WordPress file names.

Adding a file name extension to a “post” is easy. Just go into the permalink settings and use the custom structure setting, replacing the trailing slash with “.” and the file name extension of your choice.

With pages, category pages and tag pages, you need a group of functions and filters. As far as I know, the plugins available for file name extensions only work with pages.

Add File Name Extensions

I found an example somewhere while searching for hours. Don’t ask me where because I doubt I can find it again even if I try. I made a few changes and got this to work for me:

function hpct_page_rewrite($rules) {
  foreach ($rules as $key => $value) {
    $newrules[str_replace('/?', '.html', $key)] = $value;
  }
  return $newrules;
}
function hpct_page_link($link) {
  return $link . '.html';
}
function hpct_category_rewrite($rules) {
  foreach ($rules as $key => $value) {
    $newrules[str_replace('/?', '.html', $key)] = $value;
  }
  return $newrules;
}
function hpct_category_link($link) {
  return $link . '.html';
}
function hpct_tag_rewrite($rules) {
  foreach ($rules as $key => $value) {
    $newrules[str_replace('/?', '.html', $key)] = $value;
  }
  return $newrules;
}
function hpct_tag_link($link) {
  return $link . '.html';
}
add_filter('page_rewrite_rules', 'hpct_page_rewrite', 3);
add_filter('page_link', 'hpct_page_link', 1);
add_filter('category_rewrite_rules', 'hpct_category_rewrite', 3);
add_filter('category_link', 'hpct_category_link', 1);
add_filter('tag_rewrite_rules', 'hpct_tag_rewrite', 3);
add_filter('tag_link', 'hpct_tag_link', 1);

function change_taxonomy_permalinks() {
  $category_rewrite = array();
  $post_tag_rewrite = array();
  add_permastruct( 'category', 'category-%category%', $category_rewrite );
  add_permastruct( 'post_tag', 'tag-%post_tag%', $post_tag_rewrite );
}
add_action('init', 'change_taxonomy_permalinks', 1);

Yes, it’s hard-coded as “.html” for all the file extensions. That’s the way I have things set up. You can use anything you want as long as your web browser recognizes them as pages.

The last function will convert the “/” to “-” for category and tag pages. The function is only necessary if you’re creating static pages (like me).

Editing Previous Articles

It’s always fun to go back and edit previous articles. I’m being sarcastic, of course. Nevertheless, I have to go back to the beginning of my WordPress Static Site Generator series (sorry, the series has been removed), make changes and point to this article.

It took me a lot longer to figure out how to change file extensions on category and tag pages than I care to admit. I want to make as many changes from within WordPress as I can without breaking anything. It makes the post-processing script easier to work with.

Share:

Facebook Twitter Google+

Subscribe:

E-Mail, RSS Feed

Categories:

Technology

Previous and Next Articles:

« »

Comments:

Your comment will appear below the form when it's approved. When the page redisplays after hitting the send button (it can take a few seconds), your comment has been sent.

When replying to someone else's comment, please start the comment with "@" and the name so I can put it in the right place.

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