FV Top Level Categories - Version 1.1.2

Version Description

  • fix for /category/child-category redirecting to /child-category page
Download this release

Release Info

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

Version 1.1.2

Files changed (2) hide show
  1. readme.txt +54 -0
  2. top-level-cats.php +156 -0
readme.txt ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === FV Top Level Categories ===
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.1
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
18
+
19
+ [Support](http://foliovision.com/seo-tools/wordpress/plugins/fv-top-level-categories)
20
+
21
+ == Installation ==
22
+
23
+ 1. Copy the `top-level-cats.php` file into your `wp-content/plugins` directory
24
+ 1. Activate the plugin through the 'Plugins' menu in WordPress
25
+ 1. That's it! :)
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
+
33
+ = How do I automatically redirect people from the old category permalink? =
34
+
35
+ We recommend that you use the [Redirection](http://wordpress.org/extend/plugins/redirection/) plugin and add your old an new category links, or use a Regex redirection rule.
36
+
37
+ == Uninstall ==
38
+
39
+ 1. Deactivate the plugin
40
+ 1. That's it! :)
41
+
42
+ == Changelog ==
43
+
44
+ = 1.1.2 =
45
+ * fix for /category/child-category redirecting to /child-category page
46
+
47
+ = 1.1.1 =
48
+ * fix for deeper nested categories
49
+
50
+ = 1.1 =
51
+ * fix for WP 3.1
52
+
53
+ = 1.0.1 =
54
+ * original version
top-level-cats.php ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
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.2
7
+ Author: Foliovision
8
+ Author URI: http://foliovision.com/
9
+ */
10
+
11
+ /*
12
+ Changelog
13
+ 1.1.2 fix for /category/child-category redirecting to /child-category page
14
+ 1.1.1 fix for deeper nested categories
15
+ 1.1 FV Fix for WP 3.1
16
+ 1.0.1 Original version
17
+ */
18
+
19
+ // In case we're running standalone, for some odd reason
20
+ if (function_exists('add_action'))
21
+ {
22
+ register_activation_hook(__FILE__, 'top_level_cats_activate');
23
+ register_deactivation_hook(__FILE__, 'top_level_cats_deactivate');
24
+
25
+ // Setup filters
26
+ add_filter('category_rewrite_rules', 'top_level_cats_category_rewrite_rules'); /// ok
27
+ add_filter('generate_rewrite_rules', 'top_level_cats_generate_rewrite_rules');
28
+ add_filter('category_link', 'top_level_cats_category_link', 10, 2);
29
+
30
+ ///
31
+ add_filter( 'page_rewrite_rules', 'fv_page_rewrite_rules' );
32
+ add_filter( 'request', 'fv_request_page_instead_category' );
33
+
34
+ global $clean_category_rewrites, $clean_rewrites;
35
+ $clean_category_rewrites = array();
36
+ }
37
+
38
+ function fv_page_rewrite_rules( $rules ) {
39
+ unset( $rules["(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$"] );
40
+ unset( $rules["(.+?)/(feed|rdf|rss|rss2|atom)/?$"] );
41
+ unset( $rules["(.+?)/page/?([0-9]{1,})/?$"] );
42
+ unset( $rules["(.+?)(/[0-9]+)?/?$"] );
43
+ return $rules;
44
+ }
45
+
46
+ function top_level_cats_activate()
47
+ {
48
+ global $wp_rewrite;
49
+ $wp_rewrite->flush_rules();
50
+ }
51
+
52
+ function top_level_cats_deactivate()
53
+ {
54
+ // Remove the filters so we don't regenerate the wrong rules when we flush
55
+ remove_filter('category_rewrite_rules', 'top_level_cats_category_rewrite_rules');
56
+ remove_filter('generate_rewrite_rules', 'top_level_cats_generate_rewrite_rules');
57
+ remove_filter('category_link', 'top_level_cats_category_link');
58
+
59
+ global $wp_rewrite;
60
+ $wp_rewrite->flush_rules();
61
+ }
62
+
63
+ function top_level_cats_generate_rewrite_rules($wp_rewrite)
64
+ {
65
+ global $clean_category_rewrites;
66
+ $wp_rewrite->rules = $wp_rewrite->rules + $clean_category_rewrites;
67
+ }
68
+
69
+ function top_level_cats_category_rewrite_rules($category_rewrite)
70
+ {
71
+ global $clean_category_rewrites;
72
+ global $wp_rewrite;
73
+
74
+ // Make sure to use verbose rules, otherwise we'll clobber our
75
+ // category permalinks with page permalinks
76
+ //$wp_rewrite->use_verbose_page_rules = true; /// disabling this will make sure posts work, it was here already
77
+
78
+ while (list($k, $v) = each($category_rewrite)) {
79
+ // Strip off the category prefix
80
+ $new_k = top_level_cats_remove_cat_base($k);
81
+ $clean_category_rewrites[$new_k] = $v;
82
+ }
83
+
84
+ foreach( $category_rewrite AS $key => $item ) {
85
+ if( stripos( $item, 'index.php?pagename' ) !== FALSE ) {
86
+ unset( $category_rewrite[$key] );
87
+ }
88
+ }
89
+
90
+ return $category_rewrite;
91
+ }
92
+
93
+ function top_level_cats_category_link($cat_link, $cat_id)
94
+ {
95
+ return top_level_cats_remove_cat_base($cat_link);
96
+ }
97
+
98
+ function top_level_cats_remove_cat_base($link)
99
+ {
100
+ $category_base = get_option('category_base');
101
+
102
+ // WP uses "category/" as the default
103
+ if ($category_base == '')
104
+ $category_base = 'category';
105
+
106
+ // 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)
107
+ if (substr($category_base, 0, 1) == '/')
108
+ $category_base = substr($category_base, 1);
109
+
110
+ $category_base .= '/';
111
+
112
+ return preg_replace('|' . $category_base . '|', '', $link, 1);
113
+ }
114
+
115
+ function fv_request_page_instead_category($query_string)
116
+ {
117
+ //echo '<!-- before '.var_export( $query_string, true ).'-->';
118
+
119
+ // enable this and disable top_level_cats_category_rewrite_rules, top_level_cats_generate_rewrite_rules to prefer categories over pages
120
+ /*if( isset( $query_string['pagename'] ) ) {
121
+ $cats = get_categories();
122
+ foreach( $cats AS $cat_item ) {
123
+ if( $cat_item->slug == $query_string['pagename'] ) {
124
+ $query_string['category_name'] = $query_string['pagename'];
125
+ unset( $query_string['pagename'] );
126
+ }
127
+ }
128
+ } */
129
+ if( isset( $query_string['category_name'] ) ) {
130
+ global $wpdb;
131
+
132
+ $parts = explode( '/', $query_string['category_name'] );
133
+ $slug = $wpdb->escape( trim( substr( $query_string['category_name'], strripos( $query_string['category_name'], '/' ) ), '/' ) );
134
+ $page_exists = $wpdb->get_row( "SELECT ID, post_parent FROM $wpdb->posts WHERE post_name='{$slug}' AND post_type = 'page' AND post_status = 'publish' " );
135
+
136
+ if( $page_exists && $page_exists->post_parent && stripos( $query_string['category_name'], '/' ) !== FALSE ) {
137
+ $query_string['page_id'] = $page_exists->ID;
138
+ unset( $query_string['category_name'] );
139
+ }
140
+ if( $page_exists && !$page_exists->post_parent && count( $parts ) == 1 ) { // fix for /category/child-category redirecting to /child-category page
141
+ $query_string['pagename'] = $query_string['category_name'];
142
+ unset( $query_string['category_name'] );
143
+ }
144
+ }
145
+ //echo '<!-- after '.var_export( $query_string, true ).'-->';
146
+
147
+ return $query_string; // end
148
+ }
149
+
150
+ /*function fv_redirect_canonical( $url ) {
151
+ if( $url == 'http://localhost/wordpress-3.1/uncategorized/child' ) return false;
152
+ return $url;
153
+ }
154
+ add_filter( 'redirect_canonical', 'fv_redirect_canonical' );*/
155
+
156
+ ?>