Search code examples
wordpressencodewordpress-jetpack

How to encode “?” in image URL


I checked this already and found a solution here (Q 34160546) which sadly does not work for me though, maybe anyone else has an idea?

I checked my site on pingdom and wanted to get rid of this problem they put up:

Resources with a “?” in the URL are not cached by some proxy caching servers. Remove the query string and encode the parameters into the URL for the following resources:

A bunch of jpgs are listed, but to keep it simple lets just look at my profile picture on the main homepage (right widget):

https://example.com/profile.jpg?w=192

This ? should be encoded with %3F and my question is how I can do that the best way? Within the widget code that generates the code above, I only included this:

<img src="https://example.com/profile.jpg" class="aligncenter" width="192">

Now I can’t just go ahead and write this instead, it will result in the picture not showing up:

<img src="https://example.com/profile.jpg%3Fw=192">

Can anyone tell me a quick fix for my ~25 images? Maybe it will improve the site speed..


Solution

  • In WordPress, this is possible to remove "?" from script and styles. But from the image, you need some customization.

    you can use preg_replace, I did this little example for you.

    <?php
    $url = "http://www.test/img/FMR.jpg?Qq_0y12h";
    
    echo "url = $url\n\n";
    
    $urlFormatted = preg_replace("/\?.*$/", "", $url);
    echo "urlFormatted = $urlFormatted\n";
    
    ?>
    

    For more information,

    Or you may try this code also, not tested but this will help you. please visit

    function remove_query_string($url) {
     return remove_query_arg('w', $url);
    }
    add_filter('the_permalink', 'remove_query_string');
    

    hope this will helps you.