Old

comment_form()函數实现willin的评论表情插入

继续折腾 WordPress3.0 表单生成函数 comment_form(),这次折腾的是评论表情,用了 comment_form() 后,对于一般评论表单的代码插入非常麻烦,得再折腾才能实现之。

willin 写的评论表情插入代码是很方便的,就一个 smiley.php 文件,但现在用 comment_form() 生成表单应该怎样插入调用代码呢?

经过自己对 zBench 的升级折腾,还是给搞定了,下面说说方法

(这行是废话,为了排版而存在)

一、改造 smiley.php,改造后的代码如下:

<script type="text/javascript" language="javascript">
/* <![CDATA[ */
    function grin(tag) {
    	var myField;
    	tag = ' ' + tag + ' ';
        if (document.getElementById('comment') && document.getElementById('comment').type == 'textarea') {
    		myField = document.getElementById('comment');
    	} else {
    		return false;
    	}
    	if (document.selection) {
    		myField.focus();
    		sel = document.selection.createRange();
    		sel.text = tag;
    		myField.focus();
    	}
    	else if (myField.selectionStart || myField.selectionStart == '0') {
    		var startPos = myField.selectionStart;
    		var endPos = myField.selectionEnd;
    		var cursorPos = endPos;
    		myField.value = myField.value.substring(0, startPos)
    					  + tag
    					  + myField.value.substring(endPos, myField.value.length);
    		cursorPos += tag.length;
    		myField.focus();
    		myField.selectionStart = cursorPos;
    		myField.selectionEnd = cursorPos;
    	}
    	else {
    		myField.value += tag;
    		myField.focus();
    	}
    }
/* ]]> */
</script>
<?php $smilies = '
<a href="javascript:grin(\':?:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_question.gif" alt="" /></a>
<a href="javascript:grin(\':razz:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_razz.gif" alt="" /></a>
<a href="javascript:grin(\':sad:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_sad.gif" alt="" /></a>
<a href="javascript:grin(\':evil:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_evil.gif" alt="" /></a>
<a href="javascript:grin(\':!:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_exclaim.gif" alt="" /></a>
<a href="javascript:grin(\':smile:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_smile.gif" alt="" /></a>
<a href="javascript:grin(\':oops:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_redface.gif" alt="" /></a>
<a href="javascript:grin(\':grin:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_biggrin.gif" alt="" /></a>
<a href="javascript:grin(\':eek:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_surprised.gif" alt="" /></a>
<a href="javascript:grin(\':shock:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_eek.gif" alt="" /></a>
<a href="javascript:grin(\':???:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_confused.gif" alt="" /></a>
<a href="javascript:grin(\':cool:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_cool.gif" alt="" /></a>
<a href="javascript:grin(\':lol:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_lol.gif" alt="" /></a>
<a href="javascript:grin(\':mad:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_mad.gif" alt="" /></a>
<a href="javascript:grin(\':twisted:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_twisted.gif" alt="" /></a>
<a href="javascript:grin(\':roll:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_rolleyes.gif" alt="" /></a>
<a href="javascript:grin(\':wink:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_wink.gif" alt="" /></a>
<a href="javascript:grin(\':idea:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_idea.gif" alt="" /></a>
<a href="javascript:grin(\':arrow:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_arrow.gif" alt="" /></a>
<a href="javascript:grin(\':neutral:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_neutral.gif" alt="" /></a>
<a href="javascript:grin(\':cry:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_cry.gif" alt="" /></a>
<a href="javascript:grin(\':mrgreen:\')"><img src="'.get_bloginfo("wpurl").'/wp-includes/images/smilies/icon_mrgreen.gif" alt="" /></a>
<br />'
?>

二、(comments.php)在<?php comment_form(); ?>上面加入调用 smiley.php

<?php include(TEMPLATEPATH . '/smiley.php');?>

三、修改上篇文章说的加入 Ctrl + Enter 的 comment_form() 函数参数,变成如下:

<?php comment_form('comment_field=<p class="comment-form-comment"><textarea aria-required="true" rows="8" cols="45" name="comment" id="comment" onkeydown="if(event.ctrlKey){if(event.keyCode==13){document.getElementById(\'submit\').click();return false}};"></textarea></p><p>'.$smilies.'</p>'); ?>

效果图:

真折腾~

声明: 除非注明,ZWWoOoOo文章均为原创,转载请以链接形式标明本文地址
本文地址: http://zww.me/archives/25313

82 comments

  1. 流年 流年 Firefox 3.6.10 Windows XP

    还改造了那个php文件啊。
    嗯,跟我的方法是不同的,嗯嗯

    41楼
  2. zrqx008 zrqx008 Firefox 3.6.10 Windows 7

    发现你现在又换回默认表情了。

    42楼
  3. Justice Justice Firefox 3.6.10 Windows XP

    其实 comment_form() 中间调用了很多 action 的,可以在 functions.php 里面加 hook。

    43楼
  4. ......
  5. 平板盒子 平板盒子 Google Chrome 34.0.1847.116 Windows 8.1 x64 Edition

    我来折腾下,顺便试试新邮箱头像。

    41楼
  6. Specs Specs Google Chrome 34.0.1847.116 Windows 7

    非常好用,拿去用了,并且把默认的表情替换了

    42楼
  7. BabyBichu BabyBichu Google Chrome 33.0.1750.149 Windows 8 x64 Edition

    很有用,收藏了,刚才用了

    43楼

Leave a Reply