FV Top Level Categories - Version 1.2

Version Description

  • fix for WP 3.3
  • fix for /%categor%/%post-name% permalink structure
Download this release

Release Info

Developer FolioVision
Plugin Icon 128x128 FV Top Level Categories
Version 1.2
Comparing to
See all releases

Code changes from version 1.1.3 to 1.2

Files changed (2) hide show
  1. readme.txt +11 -7
  2. top-level-cats.php +71 -104
readme.txt CHANGED
@@ -2,21 +2,21 @@
2
  Contributors: FolioVision
3
  Donate link: http://foliovision.com/seo-tools/wordpress/plugins/fv-top-level-categories
4
  Tags: categories, permalink
5
- Requires at least: 3.1
6
- Tested up to: 3.1
7
- Stable tag: 1.3
8
 
9
- This is a fix of Top Level Category plugin for Wordpress 3.1.
10
 
11
  == Description ==
12
 
13
- This is a fix of Top Level Category plugin for Wordpress 3.1. It's purpose is to provide the same behavior as the original plugin, but in new Wordpress versions.
14
 
15
  The Top Level Categories plugin allows you to remove the prefix before the URL to your category page. For example, instead of http://foliovision.com/category/work, you can use http://foliovision.com/work for the address of "work" category. WordPress doesn't allow you to have a blank prefix for categories (they insert `category/` before the name), this plugin works around that restriction.
16
 
17
  This plugin works also if you have a permalink structure like %postname% or %category%/%postname% -- this wasn't possible in the original version. However, this feature might not work properly for child categories at this point. Test carefully!
18
 
19
- [Support](http://foliovision.com/seo-tools/wordpress/plugins/fv-top-level-categories)
20
 
21
  == Installation ==
22
 
@@ -26,7 +26,7 @@ This plugin works also if you have a permalink structure like %postname% or %cat
26
 
27
  == Known Issues / Bugs ==
28
 
29
- 1. This plugin **will not work** if you have a permalink structure like `%postname` or `%category%/%postname%` -- there is currently no workaround
30
 
31
  == Frequently Asked Questions ==
32
 
@@ -45,6 +45,10 @@ Make sure your categories have unique slugs - watch out for pages with the same
45
 
46
  == Changelog ==
47
 
 
 
 
 
48
  = 1.1.3 =
49
  * fix for deeper nested pages
50
 
2
  Contributors: FolioVision
3
  Donate link: http://foliovision.com/seo-tools/wordpress/plugins/fv-top-level-categories
4
  Tags: categories, permalink
5
+ Requires at least: 3.2.1
6
+ Tested up to: 3.3
7
+ Stable tag: trunk
8
 
9
+ This is a fix of Top Level Categories plugin for Wordpress 3.1. and above.
10
 
11
  == Description ==
12
 
13
+ This is a fix of Top Level Category plugin for Wordpress 3.1. and further versions. It's purpose is to provide the same behavior as the original plugin, but in new Wordpress versions.
14
 
15
  The Top Level Categories plugin allows you to remove the prefix before the URL to your category page. For example, instead of http://foliovision.com/category/work, you can use http://foliovision.com/work for the address of "work" category. WordPress doesn't allow you to have a blank prefix for categories (they insert `category/` before the name), this plugin works around that restriction.
16
 
17
  This plugin works also if you have a permalink structure like %postname% or %category%/%postname% -- this wasn't possible in the original version. However, this feature might not work properly for child categories at this point. Test carefully!
18
 
19
+ [Support](http://foliovision.com/support/fv-top-level-categories/)
20
 
21
  == Installation ==
22
 
26
 
27
  == Known Issues / Bugs ==
28
 
29
+ 1. Issue with paging and feed URLs when using `%postname` or `%category%/%postname%` permalink structure should be fixed.
30
 
31
  == Frequently Asked Questions ==
32
 
45
 
46
  == Changelog ==
47
 
48
+ = 1.2 =
49
+ * fix for WP 3.3
50
+ * fix for /%categor%/%post-name% permalink structure
51
+
52
  = 1.1.3 =
53
  * fix for deeper nested pages
54
 
top-level-cats.php CHANGED
@@ -3,124 +3,91 @@
3
  Plugin Name: FV Top Level Categories
4
  Plugin URI: http://foliovision.com/seo-tools/wordpress/plugins/fv-top-level-categories
5
  Description: Removes the prefix from the URL for a category. For instance, if your old category link was <code>/category/catname</code> it will now be <code>/catname</code>
6
- Version: 1.1.3
7
  Author: Foliovision
8
  Author URI: http://foliovision.com/
9
  */
10
 
11
- // In case we're running standalone, for some odd reason
12
- if (function_exists('add_action'))
13
- {
14
- register_activation_hook(__FILE__, 'top_level_cats_activate');
15
- register_deactivation_hook(__FILE__, 'top_level_cats_deactivate');
16
-
17
- // Setup filters
18
- add_filter('category_rewrite_rules', 'top_level_cats_category_rewrite_rules'); /// ok
19
- add_filter('generate_rewrite_rules', 'top_level_cats_generate_rewrite_rules');
20
- add_filter('category_link', 'top_level_cats_category_link', 10, 2);
21
-
22
- ///
23
- add_filter( 'page_rewrite_rules', 'fv_page_rewrite_rules' );
24
- add_filter( 'request', 'fv_request_page_instead_category' );
25
-
26
- global $clean_category_rewrites, $clean_rewrites;
27
- $clean_category_rewrites = array();
28
- }
29
-
30
- function fv_page_rewrite_rules( $rules ) {
31
- unset( $rules["(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$"] );
32
- unset( $rules["(.+?)/(feed|rdf|rss|rss2|atom)/?$"] );
33
- unset( $rules["(.+?)/page/?([0-9]{1,})/?$"] );
34
- unset( $rules["(.+?)(/[0-9]+)?/?$"] );
35
- return $rules;
36
- }
37
-
38
- function top_level_cats_activate()
39
- {
40
  global $wp_rewrite;
41
  $wp_rewrite->flush_rules();
42
  }
43
-
44
- function top_level_cats_deactivate()
45
- {
46
- // Remove the filters so we don't regenerate the wrong rules when we flush
47
- remove_filter('category_rewrite_rules', 'top_level_cats_category_rewrite_rules');
48
- remove_filter('generate_rewrite_rules', 'top_level_cats_generate_rewrite_rules');
49
- remove_filter('category_link', 'top_level_cats_category_link');
50
-
51
- global $wp_rewrite;
52
- $wp_rewrite->flush_rules();
53
  }
54
 
55
- function top_level_cats_generate_rewrite_rules($wp_rewrite)
56
- {
57
- global $clean_category_rewrites;
58
- $wp_rewrite->rules = $wp_rewrite->rules + $clean_category_rewrites;
 
59
  }
60
 
61
- function top_level_cats_category_rewrite_rules($category_rewrite)
62
- {
63
- global $clean_category_rewrites;
64
- global $wp_rewrite;
65
-
66
- // Make sure to use verbose rules, otherwise we'll clobber our
67
- // category permalinks with page permalinks
68
- //$wp_rewrite->use_verbose_page_rules = true; /// disabling this will make sure posts work, it was here already
69
-
70
- while (list($k, $v) = each($category_rewrite)) {
71
- // Strip off the category prefix
72
- $new_k = top_level_cats_remove_cat_base($k);
73
- $clean_category_rewrites[$new_k] = $v;
74
- }
75
-
76
- foreach( $category_rewrite AS $key => $item ) {
77
- if( stripos( $item, 'index.php?pagename' ) !== FALSE ) {
78
- unset( $category_rewrite[$key] );
79
- }
80
  }
81
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  return $category_rewrite;
83
  }
84
 
85
- function top_level_cats_category_link($cat_link, $cat_id)
86
- {
87
- return top_level_cats_remove_cat_base($cat_link);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  }
89
-
90
- function top_level_cats_remove_cat_base($link)
91
- {
92
- $category_base = get_option('category_base');
93
-
94
- // WP uses "category/" as the default
95
- if ($category_base == '')
96
- $category_base = 'category';
97
-
98
- // Remove initial slash, if there is one (we remove the trailing slash in the regex replacement and don't want to end up short a slash)
99
- if (substr($category_base, 0, 1) == '/')
100
- $category_base = substr($category_base, 1);
101
-
102
- $category_base .= '/';
103
-
104
- return preg_replace('|' . $category_base . '|', '', $link, 1);
105
- }
106
-
107
- function fv_request_page_instead_category($query_string)
108
- {
109
- //echo '<!-- before '.var_export( $query_string, true ).'-->';
110
-
111
- if( isset( $query_string['category_name'] ) ) {
112
- $test_query = new WP_Query( 'pagename='.$query_string['category_name'] );
113
- //echo '<!-- test_query '.var_export( $test_query->post_count, true ).'-->';
114
- if( $test_query->post_count ) {
115
- $query_string['pagename'] = $query_string['category_name'];
116
- unset( $query_string['category_name'] );
117
- //echo '<!-- after '.var_export( $query_string, true ).'-->';
118
- return $query_string;
119
- }
120
- }
121
- //echo '<!-- after '.var_export( $query_string, true ).'-->';
122
-
123
- return $query_string; // end
124
- }
125
-
126
  ?>
3
  Plugin Name: FV Top Level Categories
4
  Plugin URI: http://foliovision.com/seo-tools/wordpress/plugins/fv-top-level-categories
5
  Description: Removes the prefix from the URL for a category. For instance, if your old category link was <code>/category/catname</code> it will now be <code>/catname</code>
6
+ Version: 1.2
7
  Author: Foliovision
8
  Author URI: http://foliovision.com/
9
  */
10
 
11
+ register_activation_hook(__FILE__,'fv_top_level_categories_refresh_rules');
12
+ add_action('created_category','fv_top_level_categories_refresh_rules');
13
+ add_action('edited_category','fv_top_level_categories_refresh_rules');
14
+ add_action('delete_category','fv_top_level_categories_refresh_rules');
15
+ function fv_top_level_categories_refresh_rules() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  global $wp_rewrite;
17
  $wp_rewrite->flush_rules();
18
  }
19
+ register_deactivation_hook(__FILE__,'fv_top_level_categories_deactivate');
20
+ function fv_top_level_categories_deactivate() {
21
+ remove_filter('category_rewrite_rules', 'fv_top_level_categories_refresh_rules'); // We don't want to insert our custom rules again
22
+ fv_top_level_categories_refresh_rules();
 
 
 
 
 
 
23
  }
24
 
25
+ // Remove category base
26
+ add_action('init', 'fv_top_level_categories_permastruct');
27
+ function fv_top_level_categories_permastruct() {
28
+ global $wp_rewrite;
29
+ $wp_rewrite->extra_permastructs['category'][0] = '%category%';
30
  }
31
 
32
+ // Add our custom category rewrite rules
33
+ add_filter('category_rewrite_rules', 'fv_top_level_categories_rewrite_rules');
34
+ function fv_top_level_categories_rewrite_rules($category_rewrite) {
35
+ //var_dump($category_rewrite); // For Debugging
36
+
37
+ /// First we need to get full URLs of our pages
38
+ $pages = get_pages( 'number=0' );
39
+ $pages_urls = array();
40
+ foreach( $pages AS $pages_item ) {
41
+ $pages_urls[] = trim( str_replace( get_bloginfo( 'url' ), '', get_permalink( $pages_item->ID ) ), '/' );
 
 
 
 
 
 
 
 
 
42
  }
43
+ ///
44
+
45
+ $category_rewrite=array();
46
+ $categories=get_categories(array('hide_empty'=>false));
47
+ foreach($categories as $category) {
48
+ $category_nicename = $category->slug;
49
+ if ( $category->parent == $category->cat_ID ) // recursive recursion
50
+ $category->parent = 0;
51
+ elseif ($category->parent != 0 )
52
+ $category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename;
53
+
54
+ /// Let's check if any of the category full URLs matches any of the pages
55
+ if( in_array( $category_nicename, $pages_urls ) ) {
56
+ continue;
57
+ }
58
+ ///
59
+
60
+
61
+ $category_rewrite['('.$category_nicename.')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
62
+ $category_rewrite['('.$category_nicename.')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
63
+ $category_rewrite['('.$category_nicename.')/?$'] = 'index.php?category_name=$matches[1]';
64
+ }
65
+ // Redirect support from Old Category Base
66
+ global $wp_rewrite;
67
+ $old_category_base = get_option('category_base') ? get_option('category_base') : 'category';
68
+ $old_category_base = trim($old_category_base, '/');
69
+ $category_rewrite[$old_category_base.'/(.*)$'] = 'index.php?category_redirect=$matches[1]';
70
+
71
+ //var_dump($category_rewrite); // For Debugging
72
  return $category_rewrite;
73
  }
74
 
75
+ // Add 'category_redirect' query variable
76
+ add_filter('query_vars', 'fv_top_level_categories_query_vars');
77
+ function fv_top_level_categories_query_vars($public_query_vars) {
78
+ $public_query_vars[] = 'category_redirect';
79
+ return $public_query_vars;
80
+ }
81
+ // Redirect if 'category_redirect' is set
82
+ add_filter('request', 'fv_top_level_categories_request');
83
+ function fv_top_level_categories_request($query_vars) {
84
+ //print_r($query_vars); // For Debugging
85
+ if(isset($query_vars['category_redirect'])) {
86
+ $catlink = trailingslashit(get_option( 'home' )) . user_trailingslashit( $query_vars['category_redirect'], 'category' );
87
+ status_header(301);
88
+ header("Location: $catlink");
89
+ exit();
90
+ }
91
+ return $query_vars;
92
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  ?>