• UID625
  • Fans5
  • Follows1
  • Posts68

4 methods to remove the “category” in WordPress site links

More Posted time:Nov 24, 2016 16:00 PM
As a heavy user of WordPress, I care very much about every detail of WordPress websites.
I believe many web masters share the same complex as I do.
After the website is up, I found there is a “category” in the URL, which I felt quite uncomfortable with and tried every modification available to remove this word. Combined with the tutorials shared by friends on the internet and my personal summaries, the methods to remove “category” cannot go around the four effective ones, namely using the plug-ins, code implementation, function modification and fixed link modification.
I. Using plug-ins
If you do not possess good coding skills and you are concerned about the “category” in the URL, we recommend you use plug-ins. You can directly search for the desired plug-in in the WordPress background and install it. Then refresh the website and you will find that the “category” in the URL has been smoothly removed.
We recommend two plug-ins:
No category parents: The author is Milardovich. The upgraded version of the plug-in above can not only remove the prefix category before the category link, but also remove the parent-category. For example, it can change the “/category/parent-category/my-category/” to the form of “/my-category/”.
Download address: Official download link of WordPress
Advantages: Easy installation, suitable for all users. It applies to a newly-established blog, a blog that plans to use the fixed link/permanent link, and a blog that has been well indexed by search engines. No need to modify the code and no concerns for upgrading. 301 redirection is applied to redirect the original link. Prevent authority downgrading of search engines to the largest extent and prevent failed original link.
Disadvantages: The plug-in may take up some free space. Here we are not referring to it as a shortcoming because the space used is slight and should not be regarded as a shortcoming. If you are concerned about it, you can choose not to use the plug-in. If you do not care, we recommend you use it.
WP No Category Base: The author is iDope. The plug-in is a WordPress plug-in developed to remove the category in the category link of fixed links and has nearly 50,000 downloads. After the plug-in is installed and started, the original category URL is configured with the 301 redirection, which is good for SEO and minimizes the authority downgrading risks.
Download address: Official download link of WordPress
But we have to say that too many plug-ins in WordPress will lead to slow website loading. This is a defect for using plug-ins to solve problems.
II. Using code to remove “category”
Paste the code below directly, and add the code below in the theme functions.php before ?> and save it. Return to the fixed link settings in the background and click “Save Changes” twice. If the category catalog page in the foreground cannot be accessed, update the code in the fixed link settings again; Return to the catalog page and you will see that the category in the link has been removed. Access to the original link with category will also be redirected to the new link.
*Remove the category in WordPress
add_action( 'load-themes.php',  'no_category_base_refresh_rules');
add_action('created_category', 'no_category_base_refresh_rules');
add_action('edited_category', 'no_category_base_refresh_rules');
add_action('delete_category', 'no_category_base_refresh_rules');
function no_category_base_refresh_rules() {
    global $wp_rewrite;
    $wp_rewrite -> flush_rules();
// register_deactivation_hook(__FILE__, 'no_category_base_deactivate');  
// function no_category_base_deactivate() {  
//  remove_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');  
//  // We don't want to insert our custom rules again  
//  no_category_base_refresh_rules();  
// }  
// Remove category base  
add_action('init', 'no_category_base_permastruct');
function no_category_base_permastruct() {
    global $wp_rewrite, $wp_version;
    if (version_compare($wp_version, '3.4', '<')) {
        // For pre-3.4 support  
        $wp_rewrite -> extra_permastructs['category'][0] = '%category%';
    } else {
        $wp_rewrite -> extra_permastructs['category']['struct'] = '%category%';
// Add our custom category rewrite rules  
add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
function no_category_base_rewrite_rules($category_rewrite) {
    //var_dump($category_rewrite); // For Debugging
    $category_rewrite = array();
    $categories = get_categories(array('hide_empty' => false));
    foreach ($categories as $category) {
        $category_nicename = $category -> slug;
        if ($category -> parent == $category -> cat_ID)// recursive recursion
            $category -> parent = 0;
        elseif ($category -> parent != 0)
            $category_nicename = get_category_parents($category -> parent, false, '/', true) . $category_nicename;
        $category_rewrite['(' . $category_nicename . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
        $category_rewrite['(' . $category_nicename . ')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
        $category_rewrite['(' . $category_nicename . ')/?$'] = 'index.php?category_name=$matches[1]';
    // Redirect support from Old Category Base  
    global $wp_rewrite;
    $old_category_base = get_option('category_base') ? get_option('category_base') : 'category';
    $old_category_base = trim($old_category_base, '/');
    $category_rewrite[$old_category_base . '/(.*)$'] = 'index.php?category_redirect=$matches[1]';
    //var_dump($category_rewrite); // For Debugging
    return $category_rewrite;
// Add 'category_redirect' query variable  
add_filter('query_vars', 'no_category_base_query_vars');
function no_category_base_query_vars($public_query_vars) {
    $public_query_vars[] = 'category_redirect';
    return $public_query_vars;
// Redirect if 'category_redirect' is set  
add_filter('request', 'no_category_base_request');
function no_category_base_request($query_vars) {
    //print_r($query_vars); // For Debugging
    if (isset($query_vars['category_redirect'])) {
        $catlink = trailingslashit(get_option('home')) . user_trailingslashit($query_vars['category_redirect'], 'category');
        header("Location: $catlink");
    return $query_vars;

III. Modifying functions
1. Find the category-template.php in the wp-includes folder of your WordPress blog.
2. Open this file, and search for the code below (in the get_category_link()):
$catlink = $wp_rewrite->get_category_permastruct();

After it, add the following in a new line:
$catlink = str_replace('/category' , '' , $catlink);

The function of code is to replace the functions with character strings to remove the /category in the category link in the function for getting category links. In this way the prefix of /category of the category link can be removed.
3. Save and upload it to the server.
IV. Modifying the fixed link and removing the prefix category of the category link
Log in to your WordPress background, open the fixed link/permanent link setting item. Enter the half-width characters in “Optional Settings” > “Prefix of Category Link”: “.”; Save it and the prefix category can be removed.
Advantages: Easy settings, suitable for all users. Applicable to a newly-established blog, or a blog that just plans to use the fixed link/permanent link. No need to modify the code and no concerns for upgrading; No plug-ins are required and the processing power required won’t be increased.
Disadvantages: The original link cannot be opened, and the 404 error is shown. If your blog is well indexed by the search engine, or your blog has many external links in the category link, you should be very careful. Do not sacrifice a large number of external links and weights because of simple operations and time-saving and effort-saving purposes.
After the rewriting is enabled, corresponding articles may not be found under the categories and tags, so you should just shorten it.
The above four methods can effectively remove the “category” in the URL. Web masters can choose one according to your actual situation. If there are other methods, you can leave a reply in the comments area for discussions.