WordPress评论添加emoji表情

wordpress评论 默认是不支持表情的,但是有很多插件可以实现,我也相信有不少用wordpress的经验的站长都不建议你用插件,因为有些插件不少带漏洞和木马的,对后续维护很是麻烦。

那么怎么用纯代码来实现呢?

1、加载emoji表情路径文件

在评论页面(comments.php)合适的位置,引入smiley.php文件

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>
<div id=”smilelink”>
<a onclick=”javascript:grin(‘:?:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_question.gif” title=”疑问” alt=”疑问” /></a>
<a onclick=”javascript:grin(‘:razz:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_razz.gif” title=”抓狂” alt=”抓狂” /></a>
<a onclick=”javascript:grin(‘:sad:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_sad.gif” title=”汗” alt=”汗” /></a>
<a onclick=”javascript:grin(‘:evil:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_evil.gif” title=”委屈” alt=”委屈” /></a>
<a onclick=”javascript:grin(‘:!:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_exclaim.gif” title=”鄙视” alt=”鄙视” /></a>
<a onclick=”javascript:grin(‘:smile:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_smile.gif” title=”微笑” alt=”微笑” /></a>
<a onclick=”javascript:grin(‘:oops:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_redface.gif” title=”可爱” alt=”可爱” /></a>
<a onclick=”javascript:grin(‘:grin:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_biggrin.gif” title=”坏笑” alt=”坏笑” /></a>
<a onclick=”javascript:grin(‘:eek:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_surprised.gif” title=”吃惊” alt=”吃惊” /></a>
<a onclick=”javascript:grin(‘:shock:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_eek.gif” title=”狂晕” alt=”狂晕” /></a>
<a onclick=”javascript:grin(‘:???:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_confused.gif” title=”可怜” alt=”可怜” /></a>
<a onclick=”javascript:grin(‘:cool:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_cool.gif” title=”酷” alt=”酷” /></a>
<a onclick=”javascript:grin(‘:lol:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_lol.gif” title=”奸” alt=”奸” /></a>
<a onclick=”javascript:grin(‘:mad:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_mad.gif” title=”怒” alt=”怒” /></a>
<a onclick=”javascript:grin(‘:twisted:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_twisted.gif” title=”狂” alt=”狂” /></a>
<a onclick=”javascript:grin(‘:roll:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_rolleyes.gif” title=”馋” alt=”馋” /></a>
<a onclick=”javascript:grin(‘:wink:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_wink.gif” title=”调皮” alt=”调皮” /></a>
<a onclick=”javascript:grin(‘:idea:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_idea.gif” title=”色” alt=”色” /></a>
<a onclick=”javascript:grin(‘:arrow:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_arrow.gif” title=”囧” alt=”囧” /></a>
<a onclick=”javascript:grin(‘:neutral:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_neutral.gif” title=”偷笑” alt=”偷笑” /></a>
<a onclick=”javascript:grin(‘:cry:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_cry.gif” title=”哭” alt=”哭” /></a>
<a onclick=”javascript:grin(‘:mrgreen:’)”><img src=”<?php echo get_template_directory_uri(); ?>/images/smilies/icon_mrgreen.gif” title=”嘿” alt=”嘿” /></a>
</div>

2、下载emoji表情包

把emoji表情包放在/images/smilies/下面(路径名称自己定就行)emoji表情包[自己去找]

3、禁用前台与后台的相关emoji表情加载脚本,并修改css文件调整大小。加入以下代码至主题的functions.php内

 
/**
 * 替换emjo表情修复WordPress升级4.2自定义表情的各种问题
 */
function disable_emoji($plugins) {
if (is_array($plugins)) {
return array_diff($plugins, array(
'wpemoji'
));
} else {
return array();
}
}
//取当前主题下images\smilies\下表情图片路径
function custom_smilie_src($old, $img) {
return get_stylesheet_directory_uri() . '/images/smilies/' . $img;
}
function init_fixsmilie() {
global $wpsmiliestrans;
add_filter('smilies_src','fa_smilies_src',1,10);
//默认表情文本与表情图片的对应关系(可自定义修改)
$wpsmiliestrans = array(
':mrgreen:' => 'icon_mrgreen.gif',
':neutral:' => 'icon_neutral.gif',
':twisted:' => 'icon_twisted.gif',
':arrow:' => 'icon_arrow.gif',
':shock:' => 'icon_eek.gif',
':smile:' => 'icon_smile.gif',
':???:' => 'icon_confused.gif',
':cool:' => 'icon_cool.gif',
':evil:' => 'icon_evil.gif',
':grin:' => 'icon_biggrin.gif',
':idea:' => 'icon_idea.gif',
':oops:' => 'icon_redface.gif',
':razz:' => 'icon_razz.gif',
':roll:' => 'icon_rolleyes.gif',
':wink:' => 'icon_wink.gif',
':cry:' => 'icon_cry.gif',
':eek:' => 'icon_surprised.gif',
':lol:' => 'icon_lol.gif',
':mad:' => 'icon_mad.gif',
':sad:' => 'icon_sad.gif',
'8-)' => 'icon_cool.gif',
'8-O' => 'icon_eek.gif',
':-(' => 'icon_sad.gif',
':-)' => 'icon_smile.gif',
':-?' => 'icon_confused.gif',
':-D' => 'icon_biggrin.gif',
':-P' => 'icon_razz.gif',
':-o' => 'icon_surprised.gif',
':-x' => 'icon_mad.gif',
':-|' => 'icon_neutral.gif',
';-)' => 'icon_wink.gif',
'8O' => 'icon_eek.gif',
':(' => 'icon_sad.gif',
':)' => 'icon_smile.gif',
':?' => 'icon_confused.gif',
':D' => 'icon_biggrin.gif',
':P' => 'icon_razz.gif',
':o' => 'icon_surprised.gif',
':x' => 'icon_mad.gif',
':|' => 'icon_neutral.gif',
';)' => 'icon_wink.gif',
':!:' => 'icon_exclaim.gif',
':?:' => 'icon_question.gif',
);
//移除WordPress4.2版本更新所带来的Emoji钩子同时挂上主题自带的表情路径
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('admin_print_styles', 'print_emoji_styles');
remove_filter('the_content_feed', 'wp_staticize_emoji');
remove_filter('comment_text_rss', 'wp_staticize_emoji');
remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
add_filter('tiny_mce_plugins', 'disable_emoji');
add_filter('smilies_src', 'custom_smilie_src', 10, 2);
}
add_action('init', 'init_fixsmilie', 5);

————下载必看————

1、本站资源均通过互联网公开合法渠道获取,仅供阅读测试,请在下载后24小时内删除,谢谢合作!
2、版权归作者或出版社方所有,本站不对涉及的版权问题负法律责任。若版权方认为本站侵权,请联系客服或发送邮件处理。
3、本站解压密码统一为:yudouyudou
4、部分压缩为part1、part2类型的,需要全部下载到电脑才能解压
5、解压工具推荐:电脑端推荐使用WINRAR(最新版),苹果电脑端用RAR解压王。
6、关于下载速度:下载速度慢的,请开通百度网盘超级VIP会员下载,本站不提供网盘vip账号,请自重。
7、关于字幕和声音:MKV的影视资源都是内封字幕音轨,网盘播放是无法解析的,下载到本地电脑用potplayer,手机用QQ影音播放。
资源失效请添加客服微信 “ civi58 ”,并发送资源编号+资源名称,我们会及时重新补发

米时光 » WordPress评论添加emoji表情

发表评论