PluginProbe
Remove Category URL – Remove 'category' base from category permalinks / 1.2.3
Remove Category URL – Remove 'category' base from category permalinks v1.2.3
1.2.4 trunk 0.1.0 1.0.2 1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2.0 1.2.1 1.2.2 1.2.3
remove-category-url / remove-category-url.php

remove-category-url.php in Remove Category URL – Remove 'category' base from category permalinks 1.2.3, at remove-category-url.php

179 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Remove Category URL
4 * Plugin URI: https://wordpress.org/plugins/remove-category-url/
5 * Description: This plugin removes '/category' from your category URLs. (e.g. `/category/my-category/` to `/my-category/`)
6 * Version: 1.2.3
7 * Author: Themeisle
8 * Author URI: https://themeisle.com
9 * Text Domain: remove-category-url
10 * Domain Path: /languages
11 */
12
13 require_once dirname( __FILE__ ) . '/admin-notices.php';
14 Admin_Notices::instance( __FILE__ );
15
16 /* hooks */
17 register_activation_hook( __FILE__, 'remove_category_url_refresh_rules' );
18 register_deactivation_hook( __FILE__, 'remove_category_url_deactivate' );
19
20 /* actions */
21 add_action( 'created_category', 'remove_category_url_refresh_rules' );
22 add_action( 'delete_category', 'remove_category_url_refresh_rules' );
23 add_action( 'edited_category', 'remove_category_url_refresh_rules' );
24 add_action( 'init', 'remove_category_url_permastruct' );
25
26 /* filters */
27 add_filter( 'category_rewrite_rules', 'remove_category_url_rewrite_rules' );
28 add_filter( 'query_vars', 'remove_category_url_query_vars' ); // Adds 'category_redirect' query variable
29 add_filter( 'request', 'remove_category_url_request' ); // Redirects if 'category_redirect' is set
30
31 function remove_category_url_refresh_rules() {
32 global $wp_rewrite;
33 $wp_rewrite->flush_rules();
34 }
35
36 function remove_category_url_deactivate() {
37 remove_filter( 'category_rewrite_rules', 'remove_category_url_rewrite_rules' ); // We don't want to insert our custom rules again
38 remove_category_url_refresh_rules();
39 }
40
41 /**
42 * Removes category base.
43 *
44 * @return void
45 */
46 function remove_category_url_permastruct() {
47 global $wp_rewrite, $wp_version;
48
49 if ( 3.4 <= $wp_version ) {
50 $wp_rewrite->extra_permastructs['category']['struct'] = '%category%';
51 } else {
52 $wp_rewrite->extra_permastructs['category'][0] = '%category%';
53 }
54 }
55
56 /**
57 * Adds our custom category rewrite rules.
58 *
59 * @param array $category_rewrite Category rewrite rules.
60 *
61 * @return array
62 */
63 function remove_category_url_rewrite_rules( $category_rewrite ) {
64 global $wp_rewrite;
65
66 $category_rewrite = array();
67
68 /* WPML is present: temporary disable terms_clauses filter to get all categories for rewrite */
69 if ( class_exists( 'Sitepress' ) ) {
70 global $sitepress;
71
72 remove_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ), 10 );
73 $categories = get_categories( array( 'hide_empty' => false, '_icl_show_all_langs' => true ) );
74 add_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ), 10, 4 );
75 } else {
76 $categories = get_categories( array( 'hide_empty' => false ) );
77 }
78
79 foreach ( $categories as $category ) {
80 $category_nicename = $category->slug;
81 if ( $category->parent == $category->cat_ID ) {
82 $category->parent = 0;
83 } elseif ( 0 != $category->parent ) {
84 $category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename;
85 }
86 $category_rewrite[ '(' . $category_nicename . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$' ] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
87 $category_rewrite[ '(' . $category_nicename . ')/page/?([0-9]{1,})/?$' ] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
88 $category_rewrite[ '(' . $category_nicename . ')/?$' ] = 'index.php?category_name=$matches[1]';
89 }
90
91 // Redirect support from Old Category Base
92 $old_category_base = get_option( 'category_base' ) ? get_option( 'category_base' ) : 'category';
93 $old_category_base = trim( $old_category_base, '/' );
94 $category_rewrite[ $old_category_base . '/(.*)$' ] = 'index.php?category_redirect=$matches[1]';
95
96 return $category_rewrite;
97 }
98
99 function remove_category_url_query_vars( $public_query_vars ) {
100 $public_query_vars[] = 'category_redirect';
101
102 return $public_query_vars;
103 }
104
105 /**
106 * Handles category redirects.
107 *
108 * @param $query_vars Current query vars.
109 *
110 * @return array $query_vars, or void if category_redirect is present.
111 */
112 function remove_category_url_request( $query_vars ) {
113 if ( isset( $query_vars['category_redirect'] ) ) {
114 $catlink = trailingslashit( get_option( 'home' ) ) . user_trailingslashit( $query_vars['category_redirect'], 'category' );
115 status_header( 301 );
116 header( "Location: " . esc_url_raw( $catlink ) );
117 exit;
118 }
119
120 return $query_vars;
121 }
122
123 require_once trailingslashit( __DIR__ ) . '/vendor/autoload.php';
124
125
126 add_filter( 'themeisle_sdk_products', function ( $products ) {
127 $products[] = __FILE__;
128
129 return $products;
130 } );
131
132 /**
133 * Filters Themeisle SDK Black Friday / promotional notice data for this product.
134 *
135 * @param array<string, array<string, mixed>> $configs Configurations keyed by product slug.
136 * @return array<string, array<string, mixed>> Modified configs with Remove Category URL–specific notice data.
137 */
138 add_filter( 'themeisle_sdk_blackfriday_data', function ( $configs ) {
139 $config = isset( $configs['default'] ) ? $configs['default'] : array();
140
141 if ( defined( 'NEVE_VERSION' ) ) {
142 return $configs;
143 }
144
145 // translators: 1. Number of free licenses, 2. The price of the product.
146 $config['message'] = sprintf( __( 'You\'re using Remove Category URL, and the team behind it is celebrating Black Friday by giving away %1$s licences of Neve Pro. A premium WordPress theme worth %2$s, packed with starter sites, a header builder, and WooCommerce layouts. Claim yours before they run out.', 'remove-category-url' ), 100, '$69' );
147 $config['cta_label'] = __( 'Get Neve Pro free', 'remove-category-url' );
148 $config['plugin_meta_message'] = __( 'Black Friday Sale - Get Neve Pro free', 'remove-category-url' );
149 $config['sale_url'] = tsdk_translate_link( tsdk_utmify( 'https://themeisle.link/neve-claim-bf', 'bfcm', 'remove-cat-url' ) );
150
151 $configs[ basename( dirname( __FILE__ ) ) ] = $config;
152
153 return $configs;
154 } );
155
156 /**
157 * Adds plugin meta links.
158 *
159 * @param array $meta_fields The plugin meta fields.
160 * @param string $file The plugin file.
161 *
162 * @return array
163 */
164 function add_plugin_review_link( $meta_fields, $file ) {
165 if ( plugin_basename( __FILE__ ) === $file && is_array( $meta_fields ) ) {
166 $svg = '<svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-star"><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/></svg>';
167
168 $meta_fields[] = sprintf(
169 '<a href="%s" target="_blank">%s<span class="dashicons dashicons-star-filled" style="font-size: 15px;"></span></a>',
170 esc_url( 'https://wordpress.org/support/plugin/remove-category-url/reviews/#new-post' ),
171 esc_html__( 'Found it useful? Rate the plugin', 'remove-category-url' ),
172 );
173 }
174
175 return $meta_fields;
176 }
177
178 add_filter( 'plugin_row_meta', 'add_plugin_review_link', 10, 2 );
179