Created
December 5, 2013 04:41
-
-
Save pospi/7800235 to your computer and use it in GitHub Desktop.
Remove forced `/blog/` prefix added to main site permastruct in Wordpress multisite environments
This file contains hidden or 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 | |
global $WPMUPermastructFix; | |
$WPMUPermastructFix = new MultisitePermastructFix(); | |
class MultisitePermastructFix | |
{ | |
public function __construct() | |
{ | |
// start the checking process for main multisite blog. Will strip forced '/blog/' from the start. | |
add_action('generate_rewrite_rules', array($this, 'checkBlogPrefix'), PHP_INT_MAX); | |
} | |
// if the environment is what we're looking for, we will reprocess and force permalinks after Wordpress has overridden everything. | |
public function checkBlogPrefix($wp_rewrite) | |
{ | |
if (is_multisite() && !is_subdomain_install() && is_main_site()) { | |
add_action('shutdown', array($this, 'stripBlogPrefix')); | |
} | |
} | |
// strip prefix from all permastruct data | |
public function stripBlogPrefix() | |
{ | |
global $wp_rewrite; | |
$searchRegex = '@^/?blog/@'; | |
// strip from main permalink structures | |
$wp_rewrite->set_permalink_structure(preg_replace($searchRegex, '', $wp_rewrite->permalink_structure)); | |
$wp_rewrite->set_category_base(preg_replace($searchRegex, '', $wp_rewrite->get_category_permastruct())); | |
$wp_rewrite->set_tag_base(preg_replace($searchRegex, '', $wp_rewrite->get_tag_permastruct())); | |
// reprocess entire rules arrays in case any custom ones are missed. Category/tag base also doesn't seem fully updated via above. | |
$wp_rewrite->rules = $this->stripBlogPrefixArr($wp_rewrite->rules, $searchRegex); | |
$wp_rewrite->extra_rules = $this->stripBlogPrefixArr($wp_rewrite->extra_rules, $searchRegex); | |
$wp_rewrite->extra_rules_top = $this->stripBlogPrefixArr($wp_rewrite->extra_rules_top, $searchRegex); | |
// also reprocess custom permastructs since the first step stripping doesn't actually change them | |
foreach ($wp_rewrite->extra_permastructs as $structName => $struct) { | |
if ( is_array( $struct ) ) { | |
if ( count( $struct ) == 2 ) { | |
$wp_rewrite->extra_permastructs[$structName][0] = preg_replace($searchRegex, '', $struct[0]); | |
} else { | |
$wp_rewrite->extra_permastructs[$structName]['struct'] = preg_replace($searchRegex, '', $struct['struct']); | |
} | |
} else { | |
$wp_rewrite->extra_permastructs[$structName] = preg_replace($searchRegex, '', $struct); | |
} | |
} | |
// force an update | |
$wp_rewrite->flush_rules(); | |
} | |
protected function stripBlogPrefixArr($rules, $searchRegex) | |
{ | |
$newRules = $rules; | |
foreach ($rules as $k => $v) { | |
$newk = preg_replace($searchRegex, '', $k); | |
if ($newk != $k) { | |
$newRules = $this->replaceArrayKey($newRules, $k, $newk); | |
} | |
} | |
return $newRules; | |
} | |
protected function replaceArrayKey($array, $old_key, $new_key) | |
{ | |
$keys = array_keys($array); | |
if (false === $index = array_search($old_key, $keys)) { | |
return $array; | |
} | |
$keys[$index] = $new_key; | |
return array_combine($keys, array_values($array)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment