├── composer.json ├── readme.md └── remove-blog-slug.php /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "webdevstudios/remove-blog-slug", 3 | "type": "wordpress-plugin", 4 | "require": {} 5 | } 6 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Remove /blog Slug 2 | 3 | 4 | Removes the /blog slug from permalinks when you use multisite in subdirectory mode. 5 | 6 | 7 | ## How to use 8 | 9 | Install and activate on site ID 1. After flushing permalinks, go to `Settings -> Remove /blog Slug` and the options will be updated so permalinks will function without that slug forcefully prepended. 10 | 11 | 12 | ## Warnings 13 | You should only use this when you know there will be no manual url collisions. ie, network with domain mapping or if you're using a multi network plugin with no sites hanging off the main network/url. -------------------------------------------------------------------------------- /remove-blog-slug.php: -------------------------------------------------------------------------------- 1 | run that now since you\'ve flushed permalinks.', 'updated' ); 49 | 50 | } 51 | 52 | 53 | /** 54 | * Display Remove /blog Slug admin page 55 | * 56 | * @since 1.0 57 | * 58 | * @return void 59 | */ 60 | public function remove_blog_slug_page(){ 61 | 62 | $update = $this->remove_blog_slug(); 63 | 64 | echo '
'; 65 | echo '

Remove /blog Slug

'; 66 | settings_errors(); 67 | echo '
'; 68 | 69 | } 70 | 71 | 72 | /** 73 | * Process rewrite rules update 74 | * 75 | * @since 1.0 76 | * 77 | * @return bool True on success, false on failure 78 | */ 79 | protected function remove_blog_slug() { 80 | 81 | $structure = $this->update_permalink_structure(); 82 | 83 | $rules = $this->update_rewrite_rules(); 84 | 85 | $cat_base = $this->update_category_base(); 86 | 87 | $tag_base = $this->update_tag_base(); 88 | 89 | // If any options were updated, success! 90 | if ( ( $structure && $rules ) || $cat_base || $tag_base ) { 91 | 92 | add_settings_error( 'remove_blog_slug', esc_attr( 'settings_updated' ), 'Updated rules successfully!', 'updated' ); 93 | 94 | return true; 95 | 96 | } else { 97 | 98 | add_settings_error( 'remove_blog_slug', esc_attr( 'settings_updated' ), 'Failed to update rules. It\'s possible they\'ve been updated already.', 'error' ); 99 | 100 | return false; 101 | 102 | } 103 | 104 | } 105 | 106 | 107 | /** 108 | * Update 'permalink_structure' option 109 | * 110 | * @since 1.0 111 | * 112 | * @return bool True if option was updated, false on failure 113 | */ 114 | protected function update_permalink_structure() { 115 | 116 | $structure = get_option( 'permalink_structure' ); 117 | 118 | $structure = str_replace( '/blog', '', $structure ); 119 | 120 | $structure = update_option( 'permalink_structure', $structure ); 121 | 122 | return $structure; 123 | 124 | } 125 | 126 | 127 | /** 128 | * Update 'rewrite_rules' option 129 | * 130 | * @since 1.0 131 | * 132 | * @return bool True if option was updated, false on failure 133 | */ 134 | protected function update_rewrite_rules() { 135 | 136 | $rules = get_option( 'rewrite_rules' ); 137 | 138 | $new_rules = array(); 139 | 140 | // Rules are stored as the array keys so we need to loop through 141 | // and modify the key to remove 'blog/' 142 | foreach ( $rules as $key => $value ) { 143 | 144 | $new_key = str_replace( 'blog/', '', $key ); 145 | 146 | $new_rules[$new_key] = $value; 147 | 148 | } 149 | 150 | $new_rules = update_option( 'rewrite_rules', $new_rules ); 151 | 152 | return $new_rules; 153 | 154 | } 155 | 156 | 157 | /** 158 | * Update 'category_base' option 159 | * 160 | * @since 1.0.2 161 | * 162 | * @return bool True if option was updated, false on failure 163 | */ 164 | protected function update_category_base() { 165 | 166 | $cat_base = get_option( 'category_base' ); 167 | 168 | $cat_base = str_replace( 'blog/', '', $cat_base ); 169 | 170 | $cat_base = update_option( 'category_base', $cat_base ); 171 | 172 | return $cat_base; 173 | 174 | } 175 | 176 | 177 | /** 178 | * Update 'tag_base' option 179 | * 180 | * @since 1.0.2 181 | * 182 | * @return bool True if option was updated, false on failure 183 | */ 184 | protected function update_tag_base() { 185 | 186 | $tag_base = get_option( 'tag_base' ); 187 | 188 | $tag_base = str_replace( 'blog/', '', $tag_base ); 189 | 190 | $tag_base = update_option( 'tag_base', $tag_base ); 191 | 192 | return $tag_base; 193 | 194 | } 195 | 196 | 197 | } 198 | 199 | new Remove_Blog_Slug; --------------------------------------------------------------------------------