Remove category and tag base from WordPress URLs without a plugin?

Cover Image for Remove category and tag base from WordPress URLs without a plugin?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

📝 How to Remove Category and Tag Base from WordPress URLs without a Plugin

Are you tired of seeing that pesky category and tag base in your WordPress URLs? 😫 Don't worry, we've got you covered! In this blog post, we'll provide you with easy solutions to remove the category and tag base from your WordPress URLs without relying on plugins. 💪

The Problem: You want to remove the category and tag base from your WordPress URLs, but you don't want to use a plugin. You want a solution that resides inside the functions.php file to avoid any conflicts with future plugin updates or changes to WordPress default files. We understand your concerns and want to help you achieve your desired URL structure! 🌐

The Failed Attempts: We understand that you've already tried a couple of solutions, but they didn't work out for you. You mentioned that the htaccess solution provided by Mike Payne and the methods suggested by AskApache didn't do the trick. It's time to explore alternative solutions! 🚀

The Easy Solution:

  1. Open your WordPress theme's functions.php file. You can find it in your theme's directory.

  2. Before making any changes, it's strongly recommended to create a backup of the file, just in case.

Now, let's dive into the code that will help you remove the category and tag base from your WordPress URLs:

/**
 * Remove Category and Tag Base from WordPress URLs
 */
function remove_category_tag_base() {
  global $wp_rewrite;
  
  $wp_rewrite->extra_permastructs['category']['struct'] = '%category%';
  $wp_rewrite->extra_permastructs['post_tag']['struct'] = '%post_tag%';
}
add_action('init', 'remove_category_tag_base');

Once you've added this code snippet to your functions.php file, save the changes and refresh your WordPress site. 🔄

The Explanation:

Let's break down the code above so you can understand what's happening:

  1. We're using the remove_category_tag_base() function to modify the WordPress rewrite rules and remove the category and tag bases from the URLs. 🔄

  2. By setting the struct property of the extra_permastructs array for the category and post_tag taxonomies to their respective placeholders (%category% and %post_tag%), we effectively remove the bases from the URLs. 🔧

  3. The add_action() function is used to ensure that our custom function is hooked into the init action, allowing it to run at the appropriate time during WordPress initialization. 🕒

The Call-to-Action:

Now that you've successfully removed the category and tag base from your WordPress URLs, it's time to sit back and enjoy your cleaner and more user-friendly URLs! 🎉

If you found this guide helpful, please consider sharing it with others who might be facing the same issue. Spread the knowledge and empower your fellow WordPress enthusiasts! 👏

Have any questions, or want to share your experiences with removing the category and tag base from WordPress URLs? Drop a comment below, and let's start a conversation! 🗯️ We love hearing from our readers.

Happy URL structuring! 😎


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello