在wordpress评论中添加表情包

304次阅读
没有评论

wordpress评论 默认是不支持表情的,有很多插件可以实现,这里手动DIY来添加这个功能,使用我们自己喜欢的gif文件替换WP自带的,从而本地化。?

在wordpress评论中添加表情包

1、加载emoji表情路径文件 在评论页面(comments.php)合适的位置,引入smiley.php文件

<span class="php"><span class="hljs-meta"><?php</span> <span class="hljs-keyword">include</span>(TEMPLATEPATH . <span class="hljs-string">'/smiley.php'</span>); <span class="hljs-meta">?></span></span>

 点击预览smiley.php代码

2、下载emoji表情包 很明显上面的文件我把emoji表情包/images/smilies/下面 百度网盘下载:http://pan.baidu.com/s/1c1NQyis 3、修复表情包引入 WordPress4.2之后,使用了Emoji表情,原有自定义表情快捷符号无法再转换为表情图片gif,而我们引入的是gif文件,这样并不会显示的,所以需要禁用前台与后台的相关emoji表情加载脚本,并修改css文件调整大小。加入以下代码至主题的functions.php内:

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

这样你可以灵活的DIY,替换增加你喜欢的表情图标,文件的路径等等,轻松实现了WordPress评论插件表情功能,已经在WordPress4.7经过测试。

迷鹿, lostfawn
版权声明:本站原创文章,由迷鹿, lostfawn2021-01-01发表,共计2620字。
转载提示:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
载入中...
召唤伊斯特瓦尔