Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

WP No Category Base

By: a guest on Jun 4th, 2011  |  syntax: None  |  size: 4.41 KB  |  views: 355  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /*
  3. Plugin Name: WP No Category Base
  4. Plugin URI: http://wordpresssupplies.com/wordpress-plugins/no-category-base/
  5. Description: Removes '/category' from your category permalinks.
  6. Version: 0.7
  7. Author: iDope
  8. Author URI: http://wordpresssupplies.com/
  9. */
  10.  
  11. /*  Copyright 2008  Saurabh Gupta  (email : saurabh0@gmail.com)
  12.  
  13.     This program is free software; you can redistribute it and/or modify
  14.     it under the terms of the GNU General Public License as published by
  15.     the Free Software Foundation; either version 2 of the License, or
  16.     (at your option) any later version.
  17.  
  18.     This program is distributed in the hope that it will be useful,
  19.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.     GNU General Public License for more details.
  22.  
  23.     You should have received a copy of the GNU General Public License
  24.     along with this program; if not, write to the Free Software
  25.     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  26. */
  27.  
  28. // Refresh rules on activation/deactivation/category changes
  29. register_activation_hook(__FILE__,'no_category_base_refresh_rules');
  30. add_action('created_category','no_category_base_refresh_rules');
  31. add_action('edited_category','no_category_base_refresh_rules');
  32. add_action('delete_category','no_category_base_refresh_rules');
  33. function no_category_base_refresh_rules() {
  34.         global $wp_rewrite;
  35.         $wp_rewrite->flush_rules();
  36. }
  37. register_deactivation_hook(__FILE__,'no_category_base_deactivate');
  38. function no_category_base_deactivate() {
  39.         remove_filter('category_rewrite_rules', 'no_category_base_rewrite_rules'); // We don't want to insert our custom rules again
  40.         no_category_base_refresh_rules();
  41. }
  42.  
  43. // Remove category base
  44. add_filter('category_link', 'no_category_base',1000,2);
  45. function no_category_base($catlink, $category_id) {
  46.         $category = &get_category( $category_id );
  47.         if ( is_wp_error( $category ) )
  48.                 return $category;
  49.         $category_nicename = $category->slug;
  50.        
  51.         if ( $category->parent == $category_id ) // recursive recursion
  52.                 $category->parent = 0;
  53.         elseif ($category->parent != 0 )
  54.                 $category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename;
  55.        
  56.         $catlink = trailingslashit(get_option( 'home' )) . user_trailingslashit( $category_nicename, 'category' );
  57.         return $catlink;
  58. }
  59.  
  60. // Add our custom category rewrite rules
  61. add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
  62. function no_category_base_rewrite_rules($category_rewrite) {
  63.         //print_r($category_rewrite); // For Debugging
  64.        
  65.         $category_rewrite=array();
  66.         $categories=get_categories(array('hide_empty'=>false));
  67.         foreach($categories as $category) {
  68.                 $category_nicename = $category->slug;
  69.                 if ( $category->parent == $category->cat_ID ) // recursive recursion
  70.                         $category->parent = 0;
  71.                 elseif ($category->parent != 0 )
  72.                         $category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename;
  73.                 $category_rewrite['('.$category_nicename.')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
  74.                 $category_rewrite['('.$category_nicename.')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
  75.                 $category_rewrite['('.$category_nicename.')/?$'] = 'index.php?category_name=$matches[1]';
  76.         }
  77.         // Redirect support from Old Category Base
  78.         global $wp_rewrite;
  79.         $old_base = $wp_rewrite->get_category_permastruct();
  80.         $old_base = str_replace( '%category%', '(.+)', $old_base );
  81.         $old_base = trim($old_base, '/');
  82.         $category_rewrite[$old_base.'$'] = 'index.php?category_redirect=$matches[1]';
  83.        
  84.         //print_r($category_rewrite); // For Debugging
  85.         return $category_rewrite;
  86. }
  87.  
  88. // Add 'category_redirect' query variable
  89. add_filter('query_vars', 'no_category_base_query_vars');
  90. function no_category_base_query_vars($public_query_vars) {
  91.         $public_query_vars[] = 'category_redirect';
  92.         return $public_query_vars;
  93. }
  94. // Redirect if 'category_redirect' is set
  95. add_filter('request', 'no_category_base_request');
  96. function no_category_base_request($query_vars) {
  97.         //print_r($query_vars); // For Debugging
  98.         if(isset($query_vars['category_redirect'])) {
  99.                 $catlink = trailingslashit(get_option( 'home' )) . user_trailingslashit( $query_vars['category_redirect'], 'category' );
  100.                 status_header(301);
  101.                 header("Location: $catlink");
  102.                 exit();
  103.         }
  104.         return $query_vars;
  105. }
  106. ?>