Last active
April 21, 2018 09:21
-
-
Save janzikmund/4006141d0e98ffec9dd3 to your computer and use it in GitHub Desktop.
WordPress setting for URL permalinks structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Changes WordPress posts URL structure, so that in results in following: | |
* | |
* Articles overview: /articles/ | |
* Category overview: /articles/europe | |
* Article detail: /articles/europe/title | |
* Tag detail: /articles/tag/work | |
* | |
* Requires following settings of Permalinks: | |
* URL custom structure: /articles/%category%/%postname%/ | |
* Category base: articles/category | |
* Tag base: articles/tag | |
*/ | |
class adjustCategoryBase { | |
public $prefix = 'articles'; | |
public function __construct() { | |
// Refresh rules on activation/deactivation/category changes | |
add_action('created_category',array($this, 'no_category_base_refresh_rules')); | |
add_action('edited_category',array($this, 'no_category_base_refresh_rules')); | |
add_action('delete_category',array($this, 'no_category_base_refresh_rules')); | |
// Remove category base | |
add_action('init', array($this, 'no_category_base_permastruct')); | |
// Add our custom category rewrite rules | |
add_filter('category_rewrite_rules', array($this, 'no_category_base_rewrite_rules')); | |
} | |
// Refresh rewrite rules | |
public function no_category_base_refresh_rules() { | |
global $wp_rewrite; | |
$wp_rewrite->flush_rules(); | |
} | |
// Remove category base | |
function no_category_base_permastruct() { | |
global $wp_rewrite; | |
$permastruct = $wp_rewrite->extra_permastructs['category']['struct']; | |
$new_permastruct = str_replace('/category','',$permastruct); | |
// insert it to rules table | |
$wp_rewrite->extra_permastructs['category']['struct'] = $new_permastruct; | |
} | |
// Add our custom category rewrite rules | |
public function no_category_base_rewrite_rules($category_rewrite) { | |
$category_rewrite=array(); | |
$categories=get_categories(array('hide_empty'=>false)); | |
foreach($categories as $category) { | |
$category_nicename = $category->slug; | |
if ( $category->parent == $category->cat_ID ) // recursive recursion | |
$category->parent = 0; | |
elseif ($category->parent != 0 ) | |
$category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename; | |
$category_rewrite[$this->prefix.'/('.$category_nicename.')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]'; | |
$category_rewrite[$this->prefix.'/('.$category_nicename.')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]'; | |
$category_rewrite[$this->prefix.'/('.$category_nicename.')/?$'] = 'index.php?category_name=$matches[1]'; | |
} | |
return $category_rewrite; | |
} | |
} | |
$adjustCategoryBase = new adjustCategoryBase; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment