Skip to content

Instantly share code, notes, and snippets.

@pirate-bot
Created February 26, 2026 08:15
Show Gist options
  • Select an option

  • Save pirate-bot/3036baf0dc054fef9f153a6e9c353a94 to your computer and use it in GitHub Desktop.

Select an option

Save pirate-bot/3036baf0dc054fef9f153a6e9c353a94 to your computer and use it in GitHub Desktop.
Force removal of WordPress image sizes in urls and use the original image. This can only be used with the main Optimole plugin.
<?php
/*
Plugin Name: Optimole Tweak - Force removal of wp variations
Description: Force removal of WordPress image sizes in urls and use the original image. This can only be used with main Optimole plugin.
Version: 0.0.1
*/
function optml_post_process_fixes($html){
$extracted_urls = Optml_Manager::instance()->extract_urls_from_content( $html );
$urls = array_combine( $extracted_urls, $extracted_urls );
foreach ( $extracted_urls as $url ) {
if ( strpos( $url, Optml_Config::$service_url ) === false ) {
continue;
}
if ( preg_match( '#(-\d+x\d+(?:_c)?|(@2x)|(-scaled))\.(' . implode( '|', array_keys( Optml_Config::$image_extensions ) ) . '){1}$#i', $url, $src_parts ) ) {
$urls[ $url ] = str_replace( $src_parts[1], '', $url );
}
}
foreach ( $urls as $origin => $replace ) {
$html = preg_replace( '/(?<![\/|:|\\w])' . preg_quote( $origin, '/' ) . '/m', $replace, $html );
}
return $html;
}
add_filter( 'content_edit_pre', 'optml_post_process_fixes', 999, 1 );
add_filter( 'optml_url_post_process', 'optml_post_process_fixes',999 );
add_action(
'init',
function () {
$types = get_post_types_by_support( 'editor' );
foreach ( $types as $type ) {
$post_type = get_post_type_object( $type );
if ( property_exists( $post_type, 'show_in_rest' ) && true === $post_type->show_in_rest ) {
add_filter( 'rest_prepare_' . $type, function ( $response, $post, $request ) {
$context = $request->get_param( 'context' );
if ( $context !== 'edit' ) {
return $response;
}
$data = $response->get_data();
// Actually replace all URLs.
$data['content']['raw'] = optml_post_process_fixes( $data['content']['raw'] );
$response->set_data( $data );
return $response;
}, 10, 3 );
}
}
},
PHP_INT_MAX
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment