Visual Composer adds ?id= to background images of columns and rows

1.7k Views Asked by At

I'm trying to speed up a WordPress website which uses Visual Composer plugin. In GTmetrix result I see this:

Serve resources from a consistent URL

https://example.com/wp-content/uploads/2015/02/sfondo_form.jpg?id=15129

The image with query string ?id=XXX is background of a column in Visual Composer. How can I disable it?

All these queries are in the VC custom shortcode. Check the picture below:

enter image description here

2

There are 2 best solutions below

0
On

Background styles save in post_meta table. Here is how VC add custom CSS in a page body:

$shortcodes_custom_css = get_post_meta( $id, '_wpb_shortcodes_custom_css', true );
if ( ! empty( $shortcodes_custom_css ) ) {
    $shortcodes_custom_css = strip_tags( $shortcodes_custom_css );
    echo '<style type="text/css" data-type="vc_shortcodes-custom-css">';
    echo $shortcodes_custom_css;
    echo '</style>';
}

So we can filter post_meta value and VC output as we want:

add_filter('get_post_metadata', 'theme_prefix_vc_fix_shortcodes_custom_css', 10, 4);
function theme_prefix_vc_fix_shortcodes_custom_css($value, $object_id, $meta_key, $single) {
    if ($meta_key!='_wpb_shortcodes_custom_css' || is_admin()) {
        return $value;
    }
    // filter data for _wpb_shortcodes_custom_css only and not for admin

    remove_filter('get_post_metadata', 'theme_prefix_vc_fix_shortcodes_custom_css', 10);
    $value = get_metadata('post', $object_id, $meta_key, $single);
    // we need to remove filter here because get_metadata function use "get_post_metadata" hook so we will have loop if not remove filter
    if ($value) {
        // you can use another regular expression, this is what I created quickly
        $value = preg_replace("/(background-image[:\s]+url\(\S+\.(jpg|jpeg|png|svg|gif))\?id=\d+(\))/", "$1$3", $value);
    }
    add_filter('get_post_metadata', 'theme_prefix_vc_fix_shortcodes_custom_css', 10, 4);
    return $value;
}

Alternatively, you can use the_content filter to remove ?id=XXXX from background images in document body.

0
On

In case that might be helpful to anyone - I modified regex query to fetch all background styles:

$value = preg_replace('/(url\(\S+\.(jpg|jpeg|png|svg|gif))\?id=\d+(\))/', '$1$3', $value);