How Remove Category Base From URL In WordPress Site?


Hello Everyone,

If you are trying to remove “category” base from url  without any plugin then use my this code

URL Before Add this code  like https://raghunathgurjar.wordpress.com/category/wordpress/

URL After Add this code in your theme function like this https://raghunathgurjar.wordpress.com/wordpress/

————————————————————————————————————

/* Remove category base usign hooks */
register_activation_hook(__FILE__,    'raghunath_category_base_refresh_rules');
register_deactivation_hook(__FILE__,  'raghunath_category_base_deactivate');

/* actions */
add_action('created_category',  'raghunath_category_base_refresh_rules');
add_action('delete_category',   'raghunath_category_base_refresh_rules');
add_action('edited_category',   'raghunath_category_base_refresh_rules');
add_action('init',              'raghunath_category_base_permastruct');

/* filters */
add_filter('category_rewrite_rules', 'raghunath_category_base_rewrite_rules');
add_filter('query_vars',             'raghunath_category_base_query_vars');    // Adds 'category_redirect' query variable
add_filter('request',                'raghunath_category_base_request');       // Redirects if 'category_redirect' is set

function raghunath_category_base_refresh_rules()
{
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}

function raghunath_category_base_deactivate()
{
    remove_filter('category_rewrite_rules', 'raghunath_category_base_rewrite_rules'); // We don't want to insert our custom rules again
    raghunath_category_base_refresh_rules();
}

/**
 * Removes category base.
 *
 * @return void
 */
function raghunath_category_base_permastruct()
{
    global $wp_rewrite;
  global $wp_version;
  if ($wp_version >= 3.4) {
      $wp_rewrite->extra_permastructs['category']['struct'] = '%category%';
  } else {
    $wp_rewrite->extra_permastructs['category'][0] = '%category%';
  }
}

function raghunath_category_base_rewrite_rules($category_rewrite)
{
  $category_rewrite=array();
  /* WPML is present: temporary disable terms_clauses filter to get all categories for rewrite */
  if (class_exists('Sitepress')) {
    global $sitepress;
    remove_filter('terms_clauses', array($sitepress, 'terms_clauses'));
    $categories = get_categories(array('hide_empty' => false));
    add_filter('terms_clauses', array($sitepress, 'terms_clauses'));
  } else {
    $categories = get_categories(array('hide_empty' => false));
  }

    foreach($categories as $category) {
        $category_nicename = $category->slug;
        if ( $category->parent == $category->cat_ID ) {
            $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]';
    return $category_rewrite;
}

function raghunath_category_base_query_vars($public_query_vars)
{
    $public_query_vars[] = 'category_redirect';
    return $public_query_vars;
}

function raghunath_category_base_request($query_vars)
{
    if(isset($query_vars['category_redirect'])) {
        $catlink = trailingslashit(get_option( 'home' )) . user_trailingslashit( $query_vars['category_redirect'], 
'category' );
        status_header(301);
        header("Location: $catlink");
        exit();
    }
    return $query_vars;
}

—————————————-

Enjoy Code!

Advertisements

Sr. Software Engineer

Tagged with: , ,
Posted in Wordpress
16 comments on “How Remove Category Base From URL In WordPress Site?
  1. Wow! In the end I got a webpage from where I can really obtain valuable information regarding my study
    and knowledge.

  2. Greetings, I believe your web site might be having browser compatibility issues.

    Whenever I take a look at your website in Safari, it
    looks fine but when opening in IE, it’s got some overlapping issues. I simply wanted to give you a quick heads up! Aside from that, great website!

  3. Hello, I check your blogs daily. Your story-telling style is awesome, keep doing what you’re doing!

  4. Appreciating the time and energy you put into your blog and in depth information you offer.
    It’s good to come across a blog every once in a while that isn’t the
    same out of date rehashed information. Fantastic read!
    I’ve bookmarked your site and I’m including your
    RSS feeds to my Google account.

  5. Hey! I’m at work browsing your blog from my new iphone! Just wanted to say I love reading through your blog and look forward to all your posts! Keep up the excellent work!

    • Raghunath (Web Freelancer) says:

      Your Most Welocme!
      I am here sharing my knowledge for every one on free of cost.

      Raghunath
      Believe in Better

  6. Good day I am so delighted I found your webpage,
    I really found you by accident, while I was browsing on Askjeeve for something else, Anyways I am here now and would just like to say thanks a
    lot for a incredible post and a all round exciting blog (I also
    love the theme/design), I don’t have time to go through it all at the moment but I have saved it and also included your RSS feeds, so when I have time I will be back to read much more, Please do keep up the fantastic work.

  7. bar stools says:

    Howdy! I understand this is kind oof off-topic but I had to ask.
    Does building a well-established blog like yours take a large amount of work?
    I’m brand new to blogging however I do write in my diary everyday.
    I’d liuke to start a blog so I can share my personal experience and views online.
    Pleaqse let me know iif you have any ideas or tips for new aspiring bloggers.

    Thankyou!

  8. Habibi says:

    Wow! Thank you! I constantly needed to write on my website something like that. Can I take a fragment of your post to my blog?

    • Karen says:

      This is really interesting, You are a very skilled blogger.
      I have joined your rss feed and look forward to seeking more of your fantastic post.
      Also, I have shared your web site in my social networks!

  9. Naomi says:

    Howdy! Would you mind if I share your blog with my twitter group?

    There’s a lot of folks that I think would really enjoy your content.
    Please let me know. Many thanks

  10. Ramon says:

    Hello There. I discovered your blog the use of msn. That is a really neatly written article.

    I will make sure to bookmark it and return to learn more of your
    helpful info. Thank you for the post. I will certainly
    return.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: