/ 博客 / 7571浏览

给WordPress添加Twitter emoji表情

为了让博客显得生动些,将之前看到吐的表情插件卸载了,自己添加几行代码增加emoji颜文字。

过程参考此文:《给WordPress添加twitter emoji表情》。

1、替换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';
//可改为:return get_bloginfo('siteurl').'/wp-content/smilies/'.$img.'png';
}

下载打包好的图片文件解压到主题目录下。下载地址见:

Twitter Emoji 表情压缩包下载

2、输出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>';
//可改为: $output .= '<a class="add-smily" data-smilies="'.$alt.'"><img class="wp-smiley" src="'.get_bloginfo('siteurl').'/wp-content/smilies/'.rtrim($src_path, "gif").'png" /></a>';
    }
    return $output;
}

3、给文章编辑页面添加快捷方式

下面的代码放到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;
}

4、给评论添加表情支持

如果你用的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中的相应位置(例如:在 comment_form();后)

<?php echo fa_get_wpsmiliestrans();?>

js代码放到你的js文件中(例如:在global.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

img[class*="wp-smiley"] {
	width:18px;height:18px;vertical-align:middle;margin:0 0 0 5px;
}

5、后台评论回复添加快捷键

下面的代码放到functions.php

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把下面的内容添加进去,并放到主题根目录下

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;
});

注意点:

1、防止每次主题更换、更新后丢失smilies的方法:

get_bloginfo('template_directory').'/smilies/'

可替换为

get_bloginfo('siteurl').'wp-includes/images/smilies/'

2、原文中“js代码放到你的js文件中”,此处“你的js文件”指的是global.js、comment-reply.js等自己主题带的javascripts。

3、原文中的参考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
}

有可能无效,根据主题不同可插入:

img[class*="wp-smiley"] {
	width:16px;height:16px;vertical-align:middle;margin:0 0 0 5px;
}

应该就好啦! 😆

p.s. 不想自己折腾的可以安装插件WP Emoji One,表情比较多,但是加载速度慢。

19

  1. 大肥羊

    表情路径就看你实际的图片文件丢哪里咯 😎

    Firefox 32 · Windows 7
    1. S

      @大肥羊是的没错,哈哈!

      Chromium 37 · Ubuntu
  2. Betty

    不管是短消息还是微信,甚至是QQ,我都属于不爱发表情的那类人……
    当然,真要表情我用最多的也就两个
    一个是:〇_〇
    另个是:-_-|||||
    而你添加的表情都是萌货,不是我的菜儿

    Google Chrome 26 · Windows XP
    1. S

      @Betty我检举,你还用过⊙▽⊙!
      🙂

      WebView 1 · Samsung SM-G9009D
      1. Betty

        @S亲,来来来,我给你的留言写了,是用的最多的两个,而不是说只用这两个……

        年纪大了,眼神跟着不好使了?

        Safari 8 · Mac OS X 10.10
        1. S

          @Betty我看得清啊,我检举的也是我看到你用的嘛

          Chromium 37 · Ubuntu
  3. CONEY

    扁平化的表情,现在的流行风,都被丑鞋底拍扁啦,哈哈 😀

    Google Chrome 38 · Windows 7
    1. S

      @CONEY哈哈,苹果万岁。

      Chromium 37 · Ubuntu
  4. 一度又秋来

    😆 主要是发表之后不显示

    Google Chrome 21 · Windows XP
    1. S

      @一度又秋来什么意思?

      Chromium 37 · Ubuntu
      1. 一度又秋来

        @S没~ 弄好了 😉

        Google Chrome 21 · Windows XP
        1. S

          @一度又秋来好的😄

          Wordpress App 3 · Samsung
        2. S

          @一度又秋来我去你的博客看了,挺漂亮的,但手机留言时找不到“发表评论”

          Wordpress App 3 · Samsung
      2. 一度又秋来

        @S不知道 之前用多说时候手机还可以
        请问下 回复别人的评论不显示表情.. 😯

        Google Chrome 21 · Windows XP
        1. S

          @一度又秋来不会啊。代码里有在评论中显示的。不过也许是你的模板comment form结构不太一样。可做适当修改。

          Firefox 33 · Ubuntu
  5. 小光

    我也给typecho下的博客加上emoji表情啦。不过发现你的icon_confused.png和icon_question.png是一样的表情,是不是弄错了?在原先http://fatesinger.com/73837这里发布的是正确的文件

    Firefox 33 · Windows 8.1
    1. S

      @小光你提醒了我,仔细看了一下还真是。其实一开始我也看到了,我以为就是那样的。貌似是fatesinger的安装包里面的文件与他自己的不一样。anyway我改过来了,多谢!

      Opera developer 27 · Windows 7
  6. 微饭

    😈 后台有按钮但是木有表情图片,我简直是笑了。。。

    Google Chrome 38 · Windows 7
    1. S

      @微饭看一下图片路径对不

      Chromium 38 · Ubuntu

回复 一度又秋来 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注