文章目录[隐藏]
之前的文章:iGoogler 主题使用七牛镜像存储 WordPress 插件导致 JS 加载失败的修正,提到过一个问题:这样操作以后,该主题 JS 合并的效果消失,那么,我们何不手动合并 JS 文件呢?下面就和我一起来动手吧!最终效果是合并为两个文件:all-in-one.js 和 google-plusone.js。当然,你如果确定一定会开启 Google Plus 的话,你完全可以全部合并为 all-in-one.js。
操作步骤
1)在 wp-content/themes/iGoogler/js 目录以 UTF-8 无 BOM 模式创建 all-in-one.js,不懂如何创建的,就直接把 comments-ajax.js(comment-ajax.js)复制一份,重命名为 all-in-one.js,然后清空里面的内容即可;
2)在 wp-content/themes/iGoogler/js/all-in-one.js 中写入以下代码;
// JavaScript Document $(document).ready(function(e) { // Fix some bugs for ie6 if ('undefined' == typeof(document.body.style.maxHeight)) { if($(".wrap").width() > 1219) $(".wrap").width(1219); else if($(".wrap").width() < 1001) $(".wrap").width(1001); } // Reisize Main Content to fit height var minheight = Math.max( $("#lbar").height(), $("#rbar").height() ); if( $("#main").height() < minheight) { $("#main").height(minheight); } // Navigation $("#nav .menu li").has(".sub-menu").mouseover(function(e) { $("#nav .sub-menu:visible").css("z-index",99); $(this).children(".sub-menu").css("z-index",999); $(this).children(".sub-menu").stop().slideDown("fast"); }); $("#nav .menu li").has(".sub-menu").mouseleave(function(e) { $(this).children(".sub-menu").removeAttr('style').stop().slideUp("fast"); }); // left sidebar animation var position = $("#lbar .line").position().top + 20; $("#lbar .lbar_content").css("top", position); $("#lbar .lbar_title").click(function(e) { $(".lbar_title a").removeClass("current"); $(e.target).addClass("current"); $(".lbar_content").css("display","none"); $(this).next(".lbar_content").slideDown(500); }); }); // JavaScript Document $(document).ready(function(e) { // Fix Position problems of #respond_ad if($("#respond_ad").length > 0) { if ($("#respond_ad").offset().left < 455 ) { $("#respond_ad").offset({left:455}); } if ($("#respond").height() < $("#respond_ad").height() ) { $("#respond").height($("#respond_ad").height()); } } if($("#single_bottom_ad").length > 0) { if($("#sidebar_bottom").offset().top + $("#single_bottom_ad").height() > $(".post-nav").offset().top) { $("#sidebar_bottom").css("margin-bottom",$("#single_bottom_ad").height() - $("#sidebar_bottom").height()); } } // Single Post footer turn $("#sidebar_bottom span:first").addClass("active"); $("#sidebar_bottom ul:first").fadeIn(); $(".post-nav").css("margin-top",$("#sidebar_bottom ul:first").css("height")); $("#sidebar_bottom span").mouseover(function (){ $("#sidebar_bottom span").removeClass("active");$(this).addClass("active"); if($(this).next("ul").css("display")!="block") { $("#sidebar_bottom ul").hide(); $(this).next("ul").fadeIn(); } $("#main .post-nav").css("margin-top",$(this).next().css("height")); }); }); /** * WordPress jQuery-Ajax-Comments v1.3 by Willin Kan. * URI: http://kan.willin.org/?p=1271 */ 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('all-in-one.js'); i++ ; //Modified by me } } js_url = js_url.replace('nikbobo.qiniudn.com','www.nikbobo.net'); var edit_mode = '1', // 再編輯模式 ( '1'=開; '0'=不開 ) ajax_php_url = js_url.replace("js/all-in-one.js","comments-ajax.php"), wp_url = js_url.substr(0, js_url.indexOf('wp-content')), pic_sb = wp_url + 'wp-admin/images/wpspin_light.gif', // 提交 icon pic_no = wp_url + 'wp-admin/images/no.png', // 錯誤 icon pic_ys = wp_url + 'wp-admin/images/yes.png', // 成功 icon txt1 = '<div id="loading"><img src="' + pic_sb + '" style="vertical-align:middle;" alt=""/> 正在提交, 请稍候...</div>', txt2 = '<div id="error">#</div>', txt3 = '"><img src="' + pic_ys + '" style="vertical-align:middle;" alt=""/> 提交成功', edt1 = ', 刷新页面之前可以<a rel="nofollow" class="comment-reply-link" href="#edit" onclick=\'return addComment.moveForm("', edt2 = ')\'>再编辑</a>', cancel_edit = '取消编辑', edit, num = 1, comm_array=[]; comm_array.push(''); $(document).ready(function($) { $comments = $('#comments-title'); // 評論數的 ID $cancel = $('#cancel-comment-reply-link'); cancel_text = $cancel.text(); $submit = $('#commentform #submit'); $submit.attr('disabled', false); $('#comment').after( txt1 + txt2 ); $('#loading').hide(); $('#error').hide(); $body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body'); /** submit */ $('#commentform').submit(function() { $('#loading').slideDown(); $submit.attr('disabled', true).fadeTo('slow', 0.5); if ( edit ) $('#comment').after('<input type="text" name="edit_id" id="edit_id" value="' + edit + '" style="display:none;" />'); /** Ajax */ $.ajax( { url: ajax_php_url, data: $(this).serialize(), type: $(this).attr('method'), error: function(request) { $('#loading').slideUp(); $('#error').slideDown().html('<img src="' + pic_no + '" style="vertical-align:middle;" alt=""/> ' + request.responseText); setTimeout(function() {$submit.attr('disabled', false).fadeTo('slow', 1); $('#error').slideUp();}, 3000); }, success: function(data) { $('#loading').hide(); comm_array.push($('#comment').val()); $('textarea').each(function() {this.value = ''}); var t = addComment, cancel = t.I('cancel-comment-reply-link'), temp = t.I('wp-temp-form-div'), respond = t.I(t.respondId), post = t.I('comment_post_ID').value, parent = t.I('comment_parent').value; // comments if ( ! edit && $comments.length ) { n = parseInt($comments.text().match(/\d+/)); $comments.text($comments.text().replace( n, n + 1 )); } // show comment new_htm = '" id="new_comm_' + num + '"></'; new_htm = ( parent == '0' ) ? ('\n<ol style="clear:both;" class="commentlist' + new_htm + 'ol>') : ('\n<ul class="children' + new_htm + 'ul>'); ok_htm = '\n<span id="success_' + num + txt3; if ( edit_mode == '1' ) { div_ = (document.body.innerHTML.indexOf('div-comment-') == -1) ? '' : ((document.body.innerHTML.indexOf('li-comment-') == -1) ? 'div-' : ''); ok_htm = ok_htm.concat(edt1, div_, 'comment-', parent, '", "', parent, '", "respond", "', post, '", ', num, edt2); } ok_htm += '</span><span></span>\n'; $('#respond').before(new_htm); $('#new_comm_' + num).hide().append(data); $('#new_comm_' + num + ' li').append(ok_htm); $('#new_comm_' + num).fadeIn(4000); $body.animate( { scrollTop: $('#new_comm_' + num).offset().top - 200}, 900); countdown(); num++ ; edit = ''; $('*').remove('#edit_id'); cancel.style.display = 'none'; cancel.onclick = null; t.I('comment_parent').value = '0'; if ( temp && respond ) { temp.parentNode.insertBefore(respond, temp); temp.parentNode.removeChild(temp) } } }); // end Ajax return false; }); // end submit /** comment-reply.dev.js */ addComment = { moveForm : function(commId, parentId, respondId, postId, num) { var t = this, div, comm = t.I(commId), respond = t.I(respondId), cancel = t.I('cancel-comment-reply-link'), parent = t.I('comment_parent'), post = t.I('comment_post_ID'); if ( edit ) exit_prev_edit(); num ? ( t.I('comment').value = comm_array[num], edit = t.I('new_comm_' + num).innerHTML.match(/(comment-)(\d+)/)[2], $new_sucs = $('#success_' + num ), $new_sucs.hide(), $new_comm = $('#new_comm_' + num ), $new_comm.hide(), $cancel.text(cancel_edit) ) : $cancel.text(cancel_text); t.respondId = respondId; postId = postId || false; if ( !t.I('wp-temp-form-div') ) { div = document.createElement('div'); div.id = 'wp-temp-form-div'; div.style.display = 'none'; respond.parentNode.insertBefore(div, respond) } !comm ? ( temp = t.I('wp-temp-form-div'), t.I('comment_parent').value = '0', temp.parentNode.insertBefore(respond, temp), temp.parentNode.removeChild(temp) ) : comm.parentNode.insertBefore(respond, comm.nextSibling); $body.animate( { scrollTop: $('#respond').offset().top - 180 }, 400); if ( post && postId ) post.value = postId; parent.value = parentId; cancel.style.display = ''; cancel.onclick = function() { if ( edit ) exit_prev_edit(); var t = addComment, temp = t.I('wp-temp-form-div'), respond = t.I(t.respondId); t.I('comment_parent').value = '0'; if ( temp && respond ) { temp.parentNode.insertBefore(respond, temp); temp.parentNode.removeChild(temp); } this.style.display = 'none'; this.onclick = null; return false; }; try { t.I('comment').focus(); } catch(e) {} return false; }, I : function(e) { return document.getElementById(e); } }; // end addComment function exit_prev_edit() { $new_comm.show(); $new_sucs.show(); $('textarea').each(function() {this.value = ''}); edit = ''; } var wait = 15, submit_val = $submit.val(); function countdown() { if ( wait > 0 ) { $submit.val(wait); wait--; setTimeout(countdown, 1000); } else { $submit.val(submit_val).attr('disabled', false).fadeTo('slow', 1); wait = 15; } } });// end jQ addComment={moveForm:function(d,f,i,c){var m=this,a,h=m.I(d),b=m.I(i),l=m.I("cancel-comment-reply-link"),j=m.I("comment_parent"),k=m.I("comment_post_ID");if(!h||!b||!l||!j){return}m.respondId=i;c=c||false;if(!m.I("wp-temp-form-div")){a=document.createElement("div");a.id="wp-temp-form-div";a.style.display="none";b.parentNode.insertBefore(a,b)}h.parentNode.insertBefore(b,h.nextSibling);if(k&&c){k.value=c}j.value=f;l.style.display="";l.onclick=function(){var n=addComment,e=n.I("wp-temp-form-div"),o=n.I(n.respondId);if(!e||!o){return}n.I("comment_parent").value="0";e.parentNode.insertBefore(o,e);e.parentNode.removeChild(e);this.style.display="none";this.onclick=null;return false};try{m.I("comment").focus()}catch(g){}return false},I:function(a){return document.getElementById(a)}}; /* highlight v3 Highlights arbitrary terms. <http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html> MIT license. Johann Burkard <http://johannburkard.de> <mailto:jb@eaio.com> */ jQuery.fn.highlight = function(pat) { function innerHighlight(node, pat) { var skip = 0; if (node.nodeType == 3) { var pos = node.data.toUpperCase().indexOf(pat); if (pos >= 0) { var spannode = document.createElement('span'); spannode.className = 'highlight'; var middlebit = node.splitText(pos); var endbit = middlebit.splitText(pat.length); var middleclone = middlebit.cloneNode(true); spannode.appendChild(middleclone); middlebit.parentNode.replaceChild(spannode, middlebit); skip = 1; } } else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) { for ( var i = 0; i < node.childNodes.length; ++i) { i += innerHighlight(node.childNodes[i], pat); } } return skip; } return this.each(function() { innerHighlight(this, pat.toUpperCase()); }); }; jQuery.fn.removeHighlight = function() { return this.find("span.highlight").each(function() { this.parentNode.firstChild.nodeName; with (this.parentNode) { replaceChild(this.firstChild, this); normalize(); } }).end(); };
3)将 wp-content/themes/iGoogler/js/all-in-one.js 中的域名改成你的;
查找
js_url = js_url.replace('nikbobo.qiniudn.com','www.nikbobo.net');
其中,nikbobo.qiniudn.com 更改为你的七牛提供的二级域名,www.nikbobo.net 改成你的 WordPress 站点域名。
4)将 wp-content/themes/iGoogler 目录的 all-in-one-js.php 整个替换为以下代码;
<script type="text/javascript" src="<?php echo get_template_directory_uri();?>/js/all-in-one.js"></script> <?php 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;?>
大功告成!
开启 Google Plus 后全合并方法
1)将以下代码添加到 wp-content/themes/iGoogler/js/all-in-one.js 末尾;
(function() { var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true; po.src = 'https://apis.google.com/js/plusone.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s); })();
2)将 wp-content/themes/iGoogler 目录的 all-in-one-js.php 整个替换为以下代码;
<script type="text/javascript" src="<?php echo get_template_directory_uri();?>/js/all-in-one.js"></script> <?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;?>
大功告成!