Skip to content

Instantly share code, notes, and snippets.

@pospi
Created December 5, 2013 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pospi/7800235 to your computer and use it in GitHub Desktop.
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
<?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