| 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.4 |
| 7 |
* Author: Valerio Souza |
| 8 |
* Author URI: http://valeriosouza.com.br/ |
| 9 |
* Text Domain: remove-category-url |
| 10 |
* Domain Path: /languages |
| 11 |
*/ |
| 12 |
|
| 13 |
/* hooks */ |
| 14 |
register_activation_hook( __FILE__, 'remove_category_url_refresh_rules' ); |
| 15 |
register_deactivation_hook( __FILE__, 'remove_category_url_deactivate' ); |
| 16 |
|
| 17 |
/* actions */ |
| 18 |
add_action( 'created_category', 'remove_category_url_refresh_rules' ); |
| 19 |
add_action( 'delete_category', 'remove_category_url_refresh_rules' ); |
| 20 |
add_action( 'edited_category', 'remove_category_url_refresh_rules' ); |
| 21 |
add_action( 'init', 'remove_category_url_permastruct' ); |
| 22 |
|
| 23 |
/* filters */ |
| 24 |
add_filter( 'category_rewrite_rules', 'remove_category_url_rewrite_rules' ); |
| 25 |
add_filter( 'query_vars', 'remove_category_url_query_vars' ); // Adds 'category_redirect' query variable |
| 26 |
add_filter( 'request', 'remove_category_url_request' ); // Redirects if 'category_redirect' is set |
| 27 |
add_filter( 'plugin_row_meta', 'remove_category_url_plugin_row_meta', 10, 4 ); |
| 28 |
|
| 29 |
function remove_category_url_refresh_rules() { |
| 30 |
global $wp_rewrite; |
| 31 |
$wp_rewrite->flush_rules(); |
| 32 |
} |
| 33 |
|
| 34 |
function remove_category_url_deactivate() { |
| 35 |
remove_filter( 'category_rewrite_rules', 'remove_category_url_rewrite_rules' ); // We don't want to insert our custom rules again |
| 36 |
remove_category_url_refresh_rules(); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Removes category base. |
| 41 |
* |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
function remove_category_url_permastruct() { |
| 45 |
global $wp_rewrite, $wp_version; |
| 46 |
|
| 47 |
if ( 3.4 <= $wp_version ) { |
| 48 |
$wp_rewrite->extra_permastructs['category']['struct'] = '%category%'; |
| 49 |
} else { |
| 50 |
$wp_rewrite->extra_permastructs['category'][0] = '%category%'; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Adds our custom category rewrite rules. |
| 56 |
* |
| 57 |
* @param array $category_rewrite Category rewrite rules. |
| 58 |
* |
| 59 |
* @return array |
| 60 |
*/ |
| 61 |
function remove_category_url_rewrite_rules( $category_rewrite ) { |
| 62 |
global $wp_rewrite; |
| 63 |
|
| 64 |
$category_rewrite = array(); |
| 65 |
|
| 66 |
/* WPML is present: temporary disable terms_clauses filter to get all categories for rewrite */ |
| 67 |
if ( class_exists( 'Sitepress' ) ) { |
| 68 |
global $sitepress; |
| 69 |
|
| 70 |
remove_filter('terms_clauses', array($sitepress, 'terms_clauses'),10 ); |
| 71 |
$categories = get_categories( array( 'hide_empty' => false, '_icl_show_all_langs' => true ) ); |
| 72 |
add_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ), 10, 4 ); |
| 73 |
} else { |
| 74 |
$categories = get_categories( array( 'hide_empty' => false ) ); |
| 75 |
} |
| 76 |
|
| 77 |
foreach ( $categories as $category ) { |
| 78 |
$category_nicename = $category->slug; |
| 79 |
if ( $category->parent == $category->cat_ID ) { |
| 80 |
$category->parent = 0; |
| 81 |
} elseif ( 0 != $category->parent ) { |
| 82 |
$category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename; |
| 83 |
} |
| 84 |
$category_rewrite[ '(' . $category_nicename . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$' ] = 'index.php?category_name=$matches[1]&feed=$matches[2]'; |
| 85 |
$category_rewrite[ '(' . $category_nicename . ')/page/?([0-9]{1,})/?$' ] = 'index.php?category_name=$matches[1]&paged=$matches[2]'; |
| 86 |
$category_rewrite[ '(' . $category_nicename . ')/?$' ] = 'index.php?category_name=$matches[1]'; |
| 87 |
} |
| 88 |
|
| 89 |
// Redirect support from Old Category Base |
| 90 |
$old_category_base = get_option( 'category_base' ) ? get_option( 'category_base' ) : 'category'; |
| 91 |
$old_category_base = trim( $old_category_base, '/' ); |
| 92 |
$category_rewrite[ $old_category_base . '/(.*)$' ] = 'index.php?category_redirect=$matches[1]'; |
| 93 |
|
| 94 |
return $category_rewrite; |
| 95 |
} |
| 96 |
|
| 97 |
function remove_category_url_query_vars( $public_query_vars ) { |
| 98 |
$public_query_vars[] = 'category_redirect'; |
| 99 |
|
| 100 |
return $public_query_vars; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Handles category redirects. |
| 105 |
* |
| 106 |
* @param $query_vars Current query vars. |
| 107 |
* |
| 108 |
* @return array $query_vars, or void if category_redirect is present. |
| 109 |
*/ |
| 110 |
function remove_category_url_request( $query_vars ) { |
| 111 |
if ( isset( $query_vars['category_redirect'] ) ) { |
| 112 |
$catlink = trailingslashit( get_option( 'home' ) ) . user_trailingslashit( $query_vars['category_redirect'], 'category' ); |
| 113 |
status_header( 301 ); |
| 114 |
header( "Location: $catlink" ); |
| 115 |
exit; |
| 116 |
} |
| 117 |
|
| 118 |
return $query_vars; |
| 119 |
} |
| 120 |
|
| 121 |
function remove_category_url_plugin_row_meta( $links, $file ) { |
| 122 |
if( plugin_basename( __FILE__ ) === $file ) { |
| 123 |
$links[] = sprintf( |
| 124 |
'<a target="_blank" href="%s">%s</a>', |
| 125 |
esc_url('https://valeriosouza.com.br/donate/'), |
| 126 |
__( 'Donate', 'remove_category_url' ) |
| 127 |
); |
| 128 |
} |
| 129 |
return $links; |
| 130 |
} |
| 131 |
|