Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active August 23, 2019 00:55
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 wpmudev-sls/66933bc2840ca2b22305c322442c6f9d to your computer and use it in GitHub Desktop.
Save wpmudev-sls/66933bc2840ca2b22305c322442c6f9d to your computer and use it in GitHub Desktop.
Tempfix Smush Not Working With Image Have Full Url In _wp_attachment_file Meta
<?php
/**
* Plugin Name: [Smush Pro] - Tempfix Smush Not Working With Image Have Full Url In _wp_attachment_file Meta
* Description: [Smush Pro] - Tempfix Smush Not Working With Image Have Full Url In _wp_attachment_file Meta - 1135825746447044
* Author: Thobk @ WPMUDEV
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'plugins_loaded', 'wpmudev_sm_fix_full_url_used_in_wp_attachment_file_meta_func', 100 );
function wpmudev_sm_fix_full_url_used_in_wp_attachment_file_meta_func() {
if( defined('WP_SMUSH_VERSION') && class_exists( 'WP_Smush' ) ){
add_filter('get_post_metadata', 'wpmudev_sm_fix_full_url_used_in_wp_attachment_file_meta', 10, 3);
function wpmudev_sm_fix_full_url_used_in_wp_attachment_file_meta( $check, $object_id, $meta_key ){
if( '_wp_attached_file' !== $meta_key ){
return $check;
}
$meta_type = 'post';
$meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
if ( ! $meta_cache ) {
$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
$meta_cache = $meta_cache[ $object_id ];
}
if ( ! $meta_key ) {
return $meta_cache;
}
if ( isset( $meta_cache[ $meta_key ][0] ) ) {
$image_path = $meta_cache[ $meta_key ][0];
// base upload url
static $wp_upload_baseurl;
if( ! $wp_upload_baseurl ){
$wp_upload_dir = wp_upload_dir( null, false );
$wp_upload_baseurl = trailingslashit( $wp_upload_dir['baseurl'] );
}
if( strpos( $image_path, $wp_upload_baseurl ) === 0 ){
$image_path = str_replace( $wp_upload_baseurl, '', $image_path );
}
return [$image_path];
}
return '';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment