Remove Category URL - Version 1.1

Version Description

  • Fix Erros
Download this release

Release Info

Developer valeriosza
Plugin Icon 128x128 Remove Category URL
Version 1.1
Comparing to
See all releases

Code changes from version 1.0.2 to 1.1

Files changed (2) hide show
  1. readme.txt +7 -4
  2. remove-category-url.php +78 -65
readme.txt CHANGED
@@ -1,11 +1,11 @@
1
  === Remove Category URL ===
2
- Contributors: valeriosza
3
  Tags: categories, category base, category, permalinks, URL structure, links, seo, cms, wpml, URL
4
  Requires at least: 3.1
5
- Tested up to: 4.2
6
- Stable tag: 1.0.2
7
  License: GPLv2
8
- Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=P5QTGDB64SU8E&lc=US&item_name=WordPress%20Plugins&no_note=0&cn=Adicionar%20instru%c3%a7%c3%b5es%20especiais%20para%20o%20vendedor%3a&no_shipping=1&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted
9
 
10
  This plugin removes '/category' from your category permalinks. (e.g. `/category/my-category/` to `/my-category/`)
11
 
@@ -63,6 +63,9 @@ A particular installation does not allow the rewrite feature in disabling the pl
63
 
64
  == Changelog ==
65
 
 
 
 
66
  = 1.0.2 =
67
  * Update Compatible with WPML.
68
 
1
  === Remove Category URL ===
2
+ Contributors: valeriosza, wordlab
3
  Tags: categories, category base, category, permalinks, URL structure, links, seo, cms, wpml, URL
4
  Requires at least: 3.1
5
+ Tested up to: 4.3.2
6
+ Stable tag: 1.1
7
  License: GPLv2
8
+ Donate link: http://wordlab.com.br/donate/
9
 
10
  This plugin removes '/category' from your category permalinks. (e.g. `/category/my-category/` to `/my-category/`)
11
 
63
 
64
  == Changelog ==
65
 
66
+ = 1.1 =
67
+ * Fix Erros
68
+
69
  = 1.0.2 =
70
  * Update Compatible with WPML.
71
 
remove-category-url.php CHANGED
@@ -1,37 +1,36 @@
1
  <?php
2
- /*
3
- Plugin Name: Remove Category URL
4
- Plugin URI: https://github.com/valeriosouza/remove-category-url
5
- Description: This plugin removes '/category' from your category permalinks. (e.g. `/category/my-category/` to `/my-category/`)
6
- Version: 1.0.2
7
- Author: Valerio Souza
8
- Author URI: http://valeriosouza.com.br/
9
- */
10
 
11
  /* hooks */
12
- register_activation_hook(__FILE__, 'remove_category_url_refresh_rules');
13
- register_deactivation_hook(__FILE__, 'remove_category_url_deactivate');
14
 
15
  /* actions */
16
- add_action('created_category', 'remove_category_url_refresh_rules');
17
- add_action('delete_category', 'remove_category_url_refresh_rules');
18
- add_action('edited_category', 'remove_category_url_refresh_rules');
19
- add_action('init', 'remove_category_url_permastruct');
20
 
21
  /* filters */
22
- add_filter('category_rewrite_rules', 'remove_category_url_rewrite_rules');
23
- add_filter('query_vars', 'remove_category_url_query_vars'); // Adds 'category_redirect' query variable
24
- add_filter('request', 'remove_category_url_request'); // Redirects if 'category_redirect' is set
 
25
 
26
- function remove_category_url_refresh_rules()
27
- {
28
  global $wp_rewrite;
29
  $wp_rewrite->flush_rules();
30
  }
31
 
32
- function remove_category_url_deactivate()
33
- {
34
- remove_filter('category_rewrite_rules', 'remove_category_url_rewrite_rules'); // We don't want to insert our custom rules again
35
  remove_category_url_refresh_rules();
36
  }
37
 
@@ -40,15 +39,14 @@ function remove_category_url_deactivate()
40
  *
41
  * @return void
42
  */
43
- function remove_category_url_permastruct()
44
- {
45
- global $wp_rewrite;
46
- global $wp_version;
47
- if ($wp_version >= 3.4) {
48
- $wp_rewrite->extra_permastructs['category']['struct'] = '%category%';
49
- } else {
50
- $wp_rewrite->extra_permastructs['category'][0] = '%category%';
51
- }
52
  }
53
 
54
  /**
@@ -58,41 +56,45 @@ function remove_category_url_permastruct()
58
  *
59
  * @return array
60
  */
61
- function remove_category_url_rewrite_rules($category_rewrite)
62
- {
63
- $category_rewrite=array();
64
- /* WPML is present: temporary disable terms_clauses filter to get all categories for rewrite */
65
- if (class_exists('Sitepress')) {
66
- global $sitepress;
67
- remove_filter('terms_clauses', array($sitepress, 'terms_clauses'));
68
- $categories = get_categories(array('hide_empty' => false, '_icl_show_all_langs' => true));
69
- add_filter('terms_clauses', array($sitepress, 'terms_clauses'));
70
- } else {
71
- $categories = get_categories(array('hide_empty' => false));
72
- }
73
-
74
- foreach($categories as $category) {
 
 
 
75
  $category_nicename = $category->slug;
76
- if ( $category->parent == $category->cat_ID ) {
77
  $category->parent = 0;
78
- } elseif ($category->parent != 0 ) {
79
- $category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename;
80
- }
81
- $category_rewrite['('.$category_nicename.')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
82
- $category_rewrite['('.$category_nicename.')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
83
- $category_rewrite['('.$category_nicename.')/?$'] = 'index.php?category_name=$matches[1]';
84
  }
 
85
  // Redirect support from Old Category Base
86
- global $wp_rewrite;
87
- $old_category_base = get_option('category_base') ? get_option('category_base') : 'category';
88
- $old_category_base = trim($old_category_base, '/');
89
- $category_rewrite[$old_category_base.'/(.*)$'] = 'index.php?category_redirect=$matches[1]';
90
  return $category_rewrite;
91
  }
92
 
93
- function remove_category_url_query_vars($public_query_vars)
94
- {
95
  $public_query_vars[] = 'category_redirect';
 
96
  return $public_query_vars;
97
  }
98
 
@@ -103,13 +105,24 @@ function remove_category_url_query_vars($public_query_vars)
103
  *
104
  * @return array $query_vars, or void if category_redirect is present.
105
  */
106
- function remove_category_url_request($query_vars)
107
- {
108
- if(isset($query_vars['category_redirect'])) {
109
- $catlink = trailingslashit(get_option( 'home' )) . user_trailingslashit( $query_vars['category_redirect'], 'category' );
110
- status_header(301);
111
- header("Location: $catlink");
112
- exit();
113
  }
 
114
  return $query_vars;
115
  }
 
 
 
 
 
 
 
 
 
 
 
1
  <?php
2
+ /**
3
+ * Plugin Name: Remove Category URL
4
+ * Plugin URI: http://valeriosouza.com.br/portfolio/remove-category-url/
5
+ * Description: This plugin removes '/category' from your category permalinks. (e.g. `/category/my-category/` to `/my-category/`)
6
+ * Version: 1.1
7
+ * Author: Valerio Souza, WordLab Academy
8
+ * Author URI: http://valeriosouza.com.br/
9
+ */
10
 
11
  /* hooks */
12
+ register_activation_hook( __FILE__, 'remove_category_url_refresh_rules' );
13
+ register_deactivation_hook( __FILE__, 'remove_category_url_deactivate' );
14
 
15
  /* actions */
16
+ add_action( 'created_category', 'remove_category_url_refresh_rules' );
17
+ add_action( 'delete_category', 'remove_category_url_refresh_rules' );
18
+ add_action( 'edited_category', 'remove_category_url_refresh_rules' );
19
+ add_action( 'init', 'remove_category_url_permastruct' );
20
 
21
  /* filters */
22
+ add_filter( 'category_rewrite_rules', 'remove_category_url_rewrite_rules' );
23
+ add_filter( 'query_vars', 'remove_category_url_query_vars' ); // Adds 'category_redirect' query variable
24
+ add_filter( 'request', 'remove_category_url_request' ); // Redirects if 'category_redirect' is set
25
+ add_filter( 'plugin_row_meta', 'remove_category_url_plugin_row_meta', 10, 4 );
26
 
27
+ function remove_category_url_refresh_rules() {
 
28
  global $wp_rewrite;
29
  $wp_rewrite->flush_rules();
30
  }
31
 
32
+ function remove_category_url_deactivate() {
33
+ remove_filter( 'category_rewrite_rules', 'remove_category_url_rewrite_rules' ); // We don't want to insert our custom rules again
 
34
  remove_category_url_refresh_rules();
35
  }
36
 
39
  *
40
  * @return void
41
  */
42
+ function remove_category_url_permastruct() {
43
+ global $wp_rewrite, $wp_version;
44
+
45
+ if ( 3.4 <= $wp_version ) {
46
+ $wp_rewrite->extra_permastructs['category']['struct'] = '%category%';
47
+ } else {
48
+ $wp_rewrite->extra_permastructs['category'][0] = '%category%';
49
+ }
 
50
  }
51
 
52
  /**
56
  *
57
  * @return array
58
  */
59
+ function remove_category_url_rewrite_rules( $category_rewrite ) {
60
+ global $wp_rewrite;
61
+
62
+ $category_rewrite = array();
63
+
64
+ /* WPML is present: temporary disable terms_clauses filter to get all categories for rewrite */
65
+ if ( class_exists( 'Sitepress' ) ) {
66
+ global $sitepress;
67
+
68
+ remove_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ) );
69
+ $categories = get_categories( array( 'hide_empty' => false, '_icl_show_all_langs' => true ) );
70
+ add_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ) );
71
+ } else {
72
+ $categories = get_categories( array( 'hide_empty' => false ) );
73
+ }
74
+
75
+ foreach ( $categories as $category ) {
76
  $category_nicename = $category->slug;
77
+ if ( $category->parent == $category->cat_ID ) {
78
  $category->parent = 0;
79
+ } elseif ( 0 != $category->parent ) {
80
+ $category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename;
81
+ }
82
+ $category_rewrite[ '(' . $category_nicename . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$' ] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
83
+ $category_rewrite[ '(' . $category_nicename . ')/page/?([0-9]{1,})/?$' ] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
84
+ $category_rewrite[ '(' . $category_nicename . ')/?$' ] = 'index.php?category_name=$matches[1]';
85
  }
86
+
87
  // Redirect support from Old Category Base
88
+ $old_category_base = get_option( 'category_base' ) ? get_option( 'category_base' ) : 'category';
89
+ $old_category_base = trim( $old_category_base, '/' );
90
+ $category_rewrite[ $old_category_base . '/(.*)$' ] = 'index.php?category_redirect=$matches[1]';
91
+
92
  return $category_rewrite;
93
  }
94
 
95
+ function remove_category_url_query_vars( $public_query_vars ) {
 
96
  $public_query_vars[] = 'category_redirect';
97
+
98
  return $public_query_vars;
99
  }
100
 
105
  *
106
  * @return array $query_vars, or void if category_redirect is present.
107
  */
108
+ function remove_category_url_request( $query_vars ) {
109
+ if ( isset( $query_vars['category_redirect'] ) ) {
110
+ $catlink = trailingslashit( get_option( 'home' ) ) . user_trailingslashit( $query_vars['category_redirect'], 'category' );
111
+ status_header( 301 );
112
+ header( "Location: $catlink" );
113
+ exit;
 
114
  }
115
+
116
  return $query_vars;
117
  }
118
+
119
+ function remove_category_url_plugin_row_meta( $links, $file ) {
120
+ if( plugin_basename( __FILE__ ) === $file ) {
121
+ $links[] = sprintf(
122
+ '<a target="_blank" href="%s">%s</a>',
123
+ esc_url('http://wordlab.com.br/donate/'),
124
+ __( 'Donate', 'remove_category_url' )
125
+ );
126
+ }
127
+ return $links;
128
+ }