emoji

表情這個東西怎麼説呢,可以讓評論更加生動,一定程度上能提高訪客的評論積極性。但是我一直沒弄表情,主要是自帶的表情太難看,而且比較流行的那幾套表情也 「太流行」 了,我都快看吐了,而且現在我的主題風格都是 flat,那個已經不適合,我就一直沒弄表情。

前幾天突然發現 twitter 的 emoji 表情特別好看,於是就把喜歡的下載了下來,想替換 wp 自帶的,結果發現 wp 自帶的是 gif,但這不影響什麼。要知道 Worpdress 的表情代碼不僅僅支持評論,也是支持文章的,在文章中插入表情也是很讚的,然後沒有人會去背表情代碼,所以我們要添加一個快捷方式。下面説下實現方法。

替換 Wordpress 自帶表情

下面的代碼放到 functions.php 中

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:;”>
<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 中

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 中的相應位置

<?php echo fa_get_wpsmiliestrans();?>

js 代碼放到你的 js 文件中

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

.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
}

twitter emoji 表情下載地址:360 雲盤 (提取碼:42d1)