Posted on Leave a comment

How to Remove /category/ from WordPress URLs

How to Remove category from WordPress URLs 2023

As a WordPress developer, you may have encountered the challenge of removing the “/category/” base from your website’s URLs. While this might seem like a daunting task, it can be accomplished with a few simple steps. In this article, we’ll guide you through the process, ensuring that your WordPress website’s URLs appear just the way you want them.

Why Remove /category/ from URLs?

WordPress categorizes posts into different categories, which is a useful feature for organizing content. However, the default URL structure includes “/category/” before the category name, making URLs longer and less user-friendly. By removing “/category/” from your URLs, you can create cleaner, more aesthetically pleasing links.

Prerequisites

Before you start, there are a couple of prerequisites to keep in mind:

  1. Ensure that your web hosting server supports PHP version 8.0 or above. If not, you may need to upgrade your hosting plan or server to meet this requirement. This is essential because the method we’ll use to remove “/category/” relies on PHP functions introduced in newer versions.
  2. Make sure you have access to your WordPress admin dashboard, as you’ll need it to modify the Permalinks settings and theme files.

Step 1: Adjust Permalinks Settings

The first step in removing “/category/” from your WordPress URLs involves tweaking the Permalinks settings. Follow these steps:

  • Log in to your WordPress admin dashboard.
  • On the right-hand side menu, navigate to “Settings” and click on “Permalinks.”
  • In the Permalinks page, find the “Category base” field. By default, it’s set to “/category/.”
  • Replace “/category/” with your preferred base, or simply leave it blank for the best results. A common choice is to use a single character like a hyphen or a period (e.g., “-“). This will create cleaner, more user-friendly URLs.

Step 2: Edit Your Theme’s functions.php

The second step involves editing your theme’s functions.php file to ensure that the changes you made to the Permalinks settings take effect. Here’s what you need to do:

  • In your WordPress admin dashboard, go to “Appearance” and click on “Theme Editor.”
  • In the Theme Editor, select the “functions.php” file on the right-hand side.
  • Add the following code to the end of the functions.php file:
function remove_remane_category( $string, $type )  {
if ( $type != 'single' && $type == 'category' && ( strpos( $string, 'category' ) !== false ) )          {
$url_without_category = str_replace( "/category/", "/", $string ); 
return trailingslashit( $url_without_category );          
}  
return $string; 
}     
add_filter( 'user_trailingslashit', 'remove_remane_category', 100, 2);

Save the changes.

Final Thoughts

By following these two simple steps, you can successfully remove “/category/” from your WordPress website’s URLs. This not only makes your links look cleaner but also enhances the user experience. Remember that changing your category base also allows you to customize your URLs further.

Keep in mind that altering your website’s core settings and theme files may carry some risks. Always make a backup of your website and its files before making any changes. This way, you can restore your website if something goes wrong.

With the right PHP version and these easy steps, you can improve your WordPress website’s URL structure and create a more polished user experience for your visitors. Happy customizing also read WordPress get author Biographical Info or author description! and GeneratePress Show Related Articles without Plugin

Leave a Reply