How to Remove the Category From WordPress URL?

1 year ago, Wordpress, 273 Views

WordPress CMS has many features. One of these features is adding categories to your content. By using categories, you can sort your content. In this article, we discuss how you can remove the category from the WordPress URL.

WordPress has a special structure for its categories. By default, you can see the main category in your WordPress URLs. Below, we discuss how you can change your default category or remove it from your WordPress URLs.

By default, if there are any categories, you can see “category” in your domain URL. For example, if you have a category called WordPress, its URL will be displayed as below:

www.yourdomain.com/category/wordpress

If for any reasons you want to change this URL or remove it, you need to follow the steps below:

1. Remove Category from URL

The first method to remove the category from URL is done in WordPress itself. First, you need to log in to your WordPress dashboard. From the menu hover over Settings and then click on Permalinks. In the permalinks click on Custom Structure.

2. Remove Category From WordPress URL With a Plugin

Another way to remove the category from WordPress URLs is by installing Remove Category URL plugin. One of the advantages of this plugin compared to other plugins in this genre is its lightness. Remove Category URL is a user-friendly plugin.

3. Remove category from WordPress URLs from .htaccess

Ways we mentioned above are done through plugins and WordPress Settings. However, you can manually remove the category from your WordPress URLs.

To do so, you need to make some changes to your .htaccess file. First, you need to have access to this file. (it doesn’t matter if you access it through cPanel or FTP) You just need to copy and paste the code below to your .htaccess file and save your changes:

RewriteRule ^category/(.+)$ http://www.site.com/$1 [R=301,L]

4. Remove category from WordPress URLs using code in functions.php

In this method, you need to make sure you haven’t made any changes to your Category Base settings in permalinks. Because this method looks for the word “category” and if it cannot find it, it won’t work.

First, from your WordPress menu click on Appearance and then Editor. Look for functions.php file then add the code below before the last PHP tag.

// Remove Parent Category from Child Category URL
add_filter('term_link', 'devvn_no_category_parents', 1000, 3);
function devvn_no_category_parents($url, $term, $taxonomy) {
    if($taxonomy == 'category'){
        $term_nicename = $term->slug;
        $url = trailingslashit(get_option( 'home' )) . user_trailingslashit( $term_nicename, 'category' );
    }
    return $url;
}
// Rewrite url mới
function devvn_no_category_parents_rewrite_rules($flash = false) {
    $terms = get_terms( array(
        'taxonomy' => 'category',
        'post_type' => 'post',
        'hide_empty' => false,
    ));
    if($terms && !is_wp_error($terms)){
        foreach ($terms as $term){
            $term_slug = $term->slug;
            add_rewrite_rule($term_slug.'/?$', 'index.php?category_name='.$term_slug,'top');
            add_rewrite_rule($term_slug.'/page/([0-9]{1,})/?$', 'index.php?category_name='.$term_slug.'&paged=$matches[1]','top');
            add_rewrite_rule($term_slug.'/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$', 'index.php?category_name='.$term_slug.'&feed=$matches[1]','top');
        }
    }
    if ($flash == true)
        flush_rewrite_rules(false);
}
add_action('init', 'devvn_no_category_parents_rewrite_rules');
 
/*Sửa lỗi khi tạo mới category bị 404*/
function devvn_new_category_edit_success() {
    devvn_no_category_parents_rewrite_rules(true);
}
add_action('created_category','devvn_new_category_edit_success');
add_action('edited_category','devvn_new_category_edit_success');
add_action('delete_category','devvn_new_category_edit_success');

Every method we mentioned in this article helps you to remove the category from WordPress URLs. If you don’t have any knowledge of programming we recommend choosing the other methods.

Conclusion

In this article, we discussed how to remove a category from the WordPress URL. The first way is to simply modify the permalinks in your WordPress settings. The second way is as always by installing a plugin.

Although for advanced users, we recommend editing the .htaccess file to remove a category from URL.