Created
October 16, 2020 11:09
-
-
Save rmpel/ce7176f502ae76f898c89b4302d019b1 to your computer and use it in GitHub Desktop.
WebP .htaccess files in wrong position causes problems with uploads-redirects
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 | |
function migrate_webp_express_htaccess_files() { | |
$extra = []; | |
$uploads = wp_upload_dir(); | |
$uploads = $uploads['basedir']; | |
$themes = WP_CONTENT_DIR .'/themes'; | |
$plugins = WP_CONTENT_DIR .'/plugins'; | |
$uploads_rel = str_replace(trailingslashit( ABSPATH ), '', $uploads); | |
$themes_rel = str_replace(trailingslashit( ABSPATH ), '', $themes); | |
$plugins_rel = str_replace(trailingslashit( ABSPATH ), '', $plugins); | |
foreach ([ | |
$uploads => [ 'RewriteRule ^/?' => 'RewriteRule ^'. $uploads_rel .'/' ], | |
$themes => [ 'RewriteRule ^/?' => 'RewriteRule ^'. $themes_rel .'/' ], | |
$plugins => [ 'RewriteRule ^/?' => 'RewriteRule ^'. $plugins_rel .'/' ], | |
] as $dir => $patch) { | |
$file = $dir .'/.htaccess'; | |
if (is_file($file)) { | |
@rename($file, $file. ".migrated"); | |
} | |
$file = $file. ".migrated"; | |
if (is_file($file)) { | |
if ( ! function_exists( 'extract_from_markers' ) ) { | |
require ABSPATH . 'wp-admin/includes/misc.php'; | |
} | |
$rules = extract_from_markers( $file, 'WebP Express' ); | |
if ($rules) { | |
$extra[] = "# Migrated from: " . (str_replace(".migrated", '', $file)); | |
$rules = implode("\n", $rules); | |
$extra[] = strtr($rules, $patch); | |
} | |
} | |
} | |
return $extra; | |
} | |
add_filter('rewrite_rules', function( $input ) { | |
$extra = migrate_webp_express_htaccess_files(); | |
return implode("\n", $extra) . "\n". $input; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment