今天偶然看到,我爱水煮鱼发布了七牛镜像存储 WordPress 插件,欣喜之余,立刻就用上了,可是,用上,问题就来了——JS 加载全失败了。怎么办?弃之不用?这感觉有点不爽,慢慢分析,慢慢调试,终于找到了原因——插件在判断正则上有问题,对于某些特殊写法的php文件没有过滤(例如:all-in-one.js.php)导致。下面是解决方法。(注:本解决方法通过改主题而不是改插件实现,且改动较大,目前暂未发现问题,但改动后该主题JS合并的效果消失,请酌情使用)
1)将 wp-content/themes/iGoogler/js 目录的 all-in-one.js.php 删掉;
2)将 wp-content/themes/iGoogler/js 目录的 comment-ajax.js 更名为 comments-ajax.js;
3)将 wp-content/themes/iGoogler/js 目录的 comment-reply.js 更名为 comments-reply.js;
4)将 wp-content/themes/iGoogler 目录的 all-in-one-js.php 整个替换为以下代码;
<?php // Common.js echo '<script type="text/javascript" src="' . get_template_directory_uri() . '/js/common.js"></script>' . "\n"; // if(is_single()) { echo '<script type="text/javascript" src="' . get_template_directory_uri() . '/js/single.js"></script>' . "\n"; if(comments_open()) { echo '<script type="text/javascript" src="' . get_template_directory_uri() . '/js/comments-ajax.js"></script>' . "\n"; echo '<script type="text/javascript" src="' . get_template_directory_uri() . '/js/comments-reply.js"></script>' . "\n"; } } if(search_words()) { echo '<script type="text/javascript" src="' . get_template_directory_uri() . '/js/search-highlight.js"></script>' . "\n"; } if(igoogler_option('google-plus-one')) { echo '<script type="text/javascript" src="' . get_template_directory_uri() . '/js/google-plusone.js"></script>' . "\n"; } ?> <?php if(search_words()): $arr = preg_split('/[\s,]+/', search_words()); ?> <script type="text/javascript"><!--//--><![CDATA[//><!-- $(document).ready(function(e) { <?php foreach($arr as $words):?> $(".post").highlight("<?php echo $words;?>"); <?php endforeach; ?> }); //--><!]]></script> <?php endif;?>
5)将 wp-content/themes/iGoogler/js 目录的 comments-ajax.js(comment-ajax.js 更名 的)修改一下;
查找
got = js_url.indexOf('js/all-in-one.js'); i++ ; //Modified by me
替换为
got = js_url.indexOf('comments-ajax.js'); i++ ; //Modified by me
查找
ajax_php_url = js_url.substring(0, got) + 'comments-ajax.php',
替换为
ajax_php_url = js_url.replace("js/comments-ajax.js","comments-ajax.php"),
查找
var i = 0, got = -1, len = document.getElementsByTagName('script').length; while ( i <= len && got == -1){ if("undefined" != typeof(document.getElementsByTagName('script')[i].src) ) { var js_url = document.getElementsByTagName('script')[i].src; got = js_url.indexOf('comments-ajax.js'); i++ ; //Modified by me } }
在后面添加一句
js_url = js_url.replace('nikbobo.qiniudn.com','www.nikbobo.net');
其中,nikbobo.qiniudn.com 更改为你的七牛提供的二级域名,www.nikbobo.net 改成你的 WordPress 站点域名。
即替换为
var i = 0, got = -1, len = document.getElementsByTagName('script').length; while ( i <= len && got == -1){ if("undefined" != typeof(document.getElementsByTagName('script')[i].src) ) { var js_url = document.getElementsByTagName('script')[i].src; got = js_url.indexOf('comments-ajax.js'); i++ ; //Modified by me } } js_url = js_url.replace('nikbobo.qiniudn.com','www.nikbobo.net');
好了,大功告成!