对于年轻一派的博主都喜欢个性化去折腾WordPress博客,当然对于写博文上表情也是少不了的,那么WordPress编辑器增加表情如何添加呢?即上一篇添加表格在来扩展学习下,其实当你看代码你是否会发现代码差不多?下面我们讲讲WordPress编辑器表情这块的内容知识。
方法一:比较简便的
注意把以下代码丢到主题函数文件
/*表情*/
function add_the_emoticons_button( $emoticonss ) {
array_push( $emoticonss, 'separator', 'emoticons' );
return $emoticonss;
}
add_filter( 'mce_buttons', 'add_the_emoticons_button' );
function add_the_emoticons_plugin( $plugins ) {
$plugins['emoticons'] = get_template_directory_uri() . '/js/emo.min.js';
return $plugins;
}
add_filter( 'mce_external_plugins', 'add_the_emoticons_plugin' );
请下载下面资源js以及图片
百度网盘: https://pan.baidu.com/s/1xJ6ielN_e8hO3As70-WZlg 提取码: 2upr
第二种方法
此方法来自大发
替换Wordpress 自带表情
下面的代码放到functions.php中
<code>
add_filter('smilies_src','fa_smilies_src',1,10);
function fa_smilies_src ($img_src, $img, $siteurl){
$img = rtrim($img, "gif");
return get_bloginfo('template_directory').'/smilies/'.$img.'png';
}
输出Wordpress 自带的表情库
由于一些表情代码的表情是一样的,这里需要去重一下,下面的代码加到functions.php中
function fa_get_wpsmiliestrans(){
global $wpsmiliestrans;
$wpsmilies = array_unique($wpsmiliestrans);
foreach($wpsmilies as $alt => $src_path){
$output .= '<a class="add-smily" data-smilies="'.$alt.'"><img class="wp-smiley" src="'.get_bloginfo('template_directory').'/smilies/'.rtrim($src_path, "gif").'png" /></a>';
}
return $output;
}
给文章编辑页面添加快捷方式
下面的代码放到functions.php中
add_action('media_buttons_context', 'fa_smilies_custom_button');
function fa_smilies_custom_button($context) {
$context .= '<style>.smilies-wrap{background:#fff;border: 1px solid #ccc;box-shadow: 2px 2px 3px rgba(0, 0, 0, 0.24);padding: 10px;position: absolute;top: 60px;width: 400px;display:none}.smilies-wrap img{height:24px;width:24px;cursor:pointer;margin-bottom:5px} .is-active.smilies-wrap{display:block}</style><a id="insert-media-button" style="position:relative" class="button insert-smilies add_smilies" title="添加表情" data-editor="content" href="javascript:;" rel="external nofollow" >
<span class="dashicons dashicons-admin-users"></span>
添加表情
</a><div class="smilies-wrap">'. fa_get_wpsmiliestrans() .'</div><script>jQuery(document).ready(function(){jQuery(document).on("click", ".insert-smilies",function() { if(jQuery(".smilies-wrap").hasClass("is-active")){jQuery(".smilies-wrap").removeClass("is-active");}else{jQuery(".smilies-wrap").addClass("is-active");}});jQuery(document).on("click", ".add-smily",function() { send_to_editor(" " + jQuery(this).data("smilies") + " ");jQuery(".smilies-wrap").removeClass("is-active");return false;});});</script>';
return $context;
}
给评论添加表情支持
如果你用的comment_form();下面的代码添加到functions.php中
<code>
add_filter( 'comment_form_defaults','fa_add_smilies_to_comment_form');
function fa_add_smilies_to_comment_form($default) {
$commenter = wp_get_current_commenter();
$default['comment_field'] .= '<p class="comment-form-smilies">' . fa_get_wpsmiliestrans() . '</p>';
return $default;
}
如果是自定义的则把下面的代码加到comments.php中的相应位置
<code>
<?php echo fa_get_wpsmiliestrans();?>
js代码放到你的js 文件中
<code>
jQuery(document).on("click", ".add-smily",
function() {
var myField;
tag = ' ' + jQuery(this).data("smilies") + ' ';
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();
}
return false;
});
参考css
<code>
.wp-smiley{
width:16px;height:16px;vertical-align:middle
}
.add-smily{
background:#fff;border:0;cursor:pointer
}
.add-smily .wp-smiley{
width:24px;height:24px;margin-right:5px
}
后台评论回复添加快捷键
下面的代码放到functions.php 中
<code>
add_action('wp_ajax_get_smiley', 'get_smiley_callback');
function get_smiley_callback(){
echo '<style>.wp-smiley{height:16px;width:16px;position:static!important}</style><div>' . fa_get_wpsmiliestrans() .'</div>';
die;
}
function smiley_scripts( $hook_suffix ) {
wp_enqueue_script( 'commentsmiley', get_template_directory_uri() . '/smiley.js', false, 'by-bigfa' );
}
add_action( 'admin_print_styles', 'smiley_scripts' );
新建一个smiley.js把下面的内容添加进去,并放到主题根目录下
<code>
jQuery(document).ready(function($){
var smiley_button='';
if ($('#comments-form').length || $('#activity-widget').length) {
$.get('/wp-admin/admin-ajax.php/?action=get_smiley',
function (data) {
smiley_button=data;
$('#qt_replycontent_toolbar input:last').after(smiley_button);
}
);
}
});
jQuery(document).on("click", ".add-smily",
function() {
var myField;
tag = ' ' + jQuery(this).data("smilies") + ' ';
if (document.getElementById('replycontent') && document.getElementById('replycontent').type == 'textarea') {
myField = document.getElementById('replycontent');
} 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();
}
return false;
});
最后如果你不介意插件还可以使用TinyMCE Smiley Button,今天的WordPress编辑器添加表情教程就写到这里吧。