Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 使用url更改全局在登台时获取实时wordpress图像_Jquery_Wordpress_Migration - Fatal编程技术网

Jquery 使用url更改全局在登台时获取实时wordpress图像

Jquery 使用url更改全局在登台时获取实时wordpress图像,jquery,wordpress,migration,Jquery,Wordpress,Migration,我需要从一个实时wordpress站点设置一个本地登台。实时站点包含超过200K的图像 我的想法是将JS放在包含上传文件夹的所有图像src的文件路径上 例如: 现场:http://www.example.com/wp-content/uploads/2016/01/roll-up-250x250.png 开发人员:http://www.localhost/project/wp-content/uploads/2016/01/roll-up-250x250.png 有没有办法做到这一点。这只需要完

我需要从一个实时wordpress站点设置一个本地登台。实时站点包含超过200K的图像

我的想法是将JS放在包含上传文件夹的所有图像src的文件路径上

例如:

现场:
http://www.example.com/wp-content/uploads/2016/01/roll-up-250x250.png

开发人员:
http://www.localhost/project/wp-content/uploads/2016/01/roll-up-250x250.png


有没有办法做到这一点。这只需要完成上传文件夹中的图像,而不是主题设计文件

您可以通过JavaScript循环查看image
src
值,测试它是否在“/wp content/uploads/”目录中,如果是,则为域替换字符串

// 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
[].forEach.call(images,function(img){
    // test if the src matches the pattern of containing '/wp-content/uploads'
    if(regExp.test(img.src)){
        // replace the test domain with the live domain
        img.src=img.src.replace('www.localhost/project','www.example.com');
    }
});
可能有一种更优雅的方法来进行匹配,但我故意以一种过于冗长的方式使用正则表达式,这样就可以很容易地将此代码扩展为一个简单易用的函数


另外,我刚刚在Chrome的JavaScript控制台中运行了这个程序来测试它,但它可以很容易地包含在MU插件文件中,并将
echo
d输出到页面上的

@Foxsk8很高兴能提供帮助。事实上,我自己有时也会遇到同样的问题,在我准备好你的问题之前,我从来没有真正的动机去解决它。很高兴我这么做了,因为现在我有了这个片段可以在我自己的项目中重用:)