Foxsk8 Foxsk8 - 1 month ago 4x
jQuery Question

Get live wordpress images on staging with url change global

I need setup a local staging from a live wordpress site. Live site containts over 200K images.

My idea is put JS that changes file path all images src that containts a upload folder.

For example:



Is there a any way to do that. This need be done only images that is on uploads folder not for themes design files.


You can do this with JavaScript by looping through the image src values, testing if it's in the '/wp-content/uploads/' directory and if so doing a string replacement for the domain.

// this is the pattern we match against
var regexPattern = "/wp-content/uploads/";
// turn the pattern into a regular expression
var regExp = new RegExp(regexPattern);
// get an array of the images
var images=document.querySelectorAll('img');
// loop through each array item
    // test if the src matches the pattern of containing '/wp-content/uploads'
        // replace the test domain with the live domain

There's probably a more elegant way of doing the matching but I purposefully used regex in an overly verbose way so that this code could very easily be extended into a simple to use function.

P.S. I just ran this in the JavaScript console in Chrome to test it but it could quite easily be included in a MU-plugin file and echod out into a on the page.