Remove Trailing Slash from Category Base and Tag Base

I'm working on a custom static site generator using WordPress. I can't figure out how to remove the slash that comes after the category base and the tag base. I want to replace each with a dash.

I'm currently using a post-processing PHP script and str_replace, but that means I'm stuck with whatever I hard code there (which is "category" and "tag" right now).

Any suggestions?

Topic rewrite-rules urls Wordpress

Category Web


I did a lot of digging and I came up with this code to solve the problem as well as adding ".html" to pages, categories and tags. The permalink setting takes care of posts.

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 str_replace('category/', 'category-', $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 str_replace('tag/', 'tag-', $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);

Yes, it's hard-coded as well and I'll eventually take care of it. But at least I can do this from within WordPress.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.