李锋镝的博客

  • 首页
  • 时间轴
  • 留言
  • 插件
  • 左邻右舍
  • 关于我
    • 关于我
    • 另一个网站
  • 知识库
  • 赞助
Destiny
自是人生长恨水长东
  1. 首页
  2. 原创
  3. 其他
  4. 正文

WordPress评论框增加自定义表情

2021年3月14日 19004点热度 0人点赞 5条评论

这几天去邻居家的博客转了转,发大家基本上都有了评论框的表情,遂觉得自己有点儿落伍了。本博客并没有使用主题自带的评论框,而是使用的wpDiscuz这个插件,其实这个插件自带的是有评论框表情的扩展的,不过那个需要额外花钱,一年好几十美刀,舍不得,只好自己瞎捣鼓捣鼓了。

百度了一圈,发现OwO这个表情插件大家用的多一些,虽然是好几年前的代码了,但是依旧很好使,这里不得不为原作者点赞。

这里附上OwO的源码地址:https://github.com/DIYgod/OwO

使用

这个插件使用起来倒是很简单。

第一步

引用OwO.min.js及OwO.min.css文件,以及初始化代码。

示例如下:

<script type="text/javascript" src="https://www.lifengdi.com/wp-content/themes/kratos-3.2.4/assets/js/OWO.js?ver=3.2.8" id="owo-js"></script>
<script type="text/javascript" >
var OwO_icon = new OwO({
logo: 'OωO表情',
container: document.getElementsByClassName('OwO')[0],
target: document.getElementsByClassName('ql-editor')[0],
api: 'https://www.lifengdi.com/wp-content/themes/kratos-3.2.4/inc/OwO.json',
position: 'down',
width: '100%',
maxHeight: '250px'
});
var OwO_icon1 = new OwO({
logo: 'OωO表情',
container: document.getElementsByClassName('OwO')[1],
target: document.getElementsByClassName('ql-editor')[1],
api: 'https://www.lifengdi.com/wp-content/themes/kratos-3.2.4/inc/OwO.json',
position: 'down',
width: '100%',
maxHeight: '250px'
});
</script>

第二步

找到wpDiscuz这个插件的评论框的位置,也就是wpdiscuz/forms/wpdFormAttr/Form.php这个文件,找到文件中的renderTextEditorButtons这个函数,在最下方的div标签的外边插入<div class="OwO"></div>,

这两步弄完,不出意外的话,你的评论框应该存在一个OwO表情的按钮,可以正常添加表情。

第三步

接下来就是处理表情的展示问题了。

找到主题的文章页面,插入以下代码:

// 文章评论表情
function custom_smilies_src($img_src, $img, $siteurl)
{
    return ASSET_PATH . '/static/images/smilies/' . $img;
}
add_filter('smilies_src', 'custom_smilies_src', 1, 10);

function smilies_reset(){
    global $wpsmiliestrans,$wp_smiliessearch,$wp_version;
    if(!get_option('use_smilies')||$wp_version<4.2) return;
    $wpsmiliestrans = array(
     ':hehe:' => 'hehe.png',
     ':haha:' => 'haha.png',
    ':tushe:' => 'tushe.png',
        ':a:' => 'a.png',
       ':ku:' => 'ku.png',
       ':nu:' => 'nu.png',
   ':kaixin:' => 'kaixin.png',
      ':han:' => 'han.png',
      ':lei:' => 'lei.png',
  ':heixian:' => 'heixian.png',
    ':bishi:' => 'bishi.png',
':bugaoxing:' => 'bugaoxing.png',
 ':zhenbang:' => 'zhenbang.png',
     ':qian:' => 'qian.png',
    ':yiwen:' => 'yiwen.png',
  ':yinxian:' => 'yinxian.png',
       ':tu:' => 'tu.png',
       ':yi:' => 'yi.png',
    ':weiqv:' => 'weiqv.png',
   ':huaxin:' => 'huaxin.png',
       ':hu:' => 'hu.png',
  ':xiaoyan:' => 'xiaoyan.png',
     ':leng:' => 'leng.png',
':taikaixin:' => 'taikaixin.png',
     ':meng:' => 'meng.png',
    ':huaji:' => 'huaji.png',
   ':huaji2:' => 'huaji2.png',
   ':huaji3:' => 'huaji3.gif',
   ':huaji4:' => 'huaji4.png',
   ':huaji5:' => 'huaji5.gif',
   ':huaji6:' => 'huaji6.png',
   ':huaji7:' => 'huaji7.png',
   ':huaji8:' => 'huaji8.png',
   ':huaji9:' => 'huaji9.png',
  ':huaji10:' => 'huaji10.png',
  ':huaji11:' => 'huaji11.png',
  ':huaji12:' => 'huaji12.png',
  ':huaji13:' => 'huaji13.png',
  ':huaji14:' => 'huaji14.png',
  ':huaji15:' => 'huaji15.png',
  ':huaji16:' => 'huaji16.gif',
  ':huaji17:' => 'huaji17.png',
  ':huaji18:' => 'huaji18.png',
  ':huaji19:' => 'huaji19.png',
  ':huaji20:' => 'huaji20.gif',
  ':huaji21:' => 'huaji21.gif',
  ':huaji22:' => 'huaji22.png',
  ':huaji23:' => 'huaji23.png',
':mianqiang:' => 'mianqiang.png',
 ':kuanghan:' => 'kuanghan.png',
     ':guai:' => 'guai.png',
 ':shuijiao:' => 'shuijiao.png',
   ':jingku:' => 'jingku.png',
  ':shengqi:' => 'shengqi.png',
   ':jingya:' => 'jingya.png',
      ':pen:' => 'pen.png',
    ':aixin:' => 'aixin.png',
   ':xinsui:' => 'xinsui.png',
   ':meigui:' => 'meigui.png',
     ':liwu:' => 'liwu.png',
  ':caihong:' => 'caihong.png',
     ':xxyl:' => 'xxyl.png',
      ':sun:' => 'sun.png',
    ':money:' => 'money.png',
     ':bulb:' => 'bulb.png',
      ':cup:' => 'cup.png',
     ':cake:' => 'cake.png',
    ':music:' => 'music.png',
    ':haha2:' => 'haha2.png',
      ':win:' => 'win.png',
     ':good:' => 'good.png',
      ':bad:' => 'bad.png',
       ':ok:' => 'ok.png',
     ':stop:' => 'stop.png',
   ':hahaha:' => 'hahaha.png',
   ':despise:' => 'despise.png',
   ':knife:' => 'knife.png',
   ':pouting:' => 'pouting.png',
   ':shutup:' => 'shutup.png',
   ':unhappy:' => 'unhappy.png',
   ':discovertruth:' => 'discovertruth.png',
   ':shit:' => 'shit.png',
   ':distressed:' => 'distressed.png',
   ':shy:' => 'shy.png',
   ':speechless:' => 'speechless.png',
   ':confuse:' => 'confuse.png',
   ':frown:' => 'frown.png',
   ':proud:' => 'proud.png',
   ':cheer:' => 'cheer.png',
   ':meditation:' => 'meditation.png',
   ':diving:' => 'diving.png',   
   ':cryingface:' => 'cryingface.png',
   ':lurking:' => 'lurking.png',
   ':thumb:' => 'thumb.png',
   ':smile:' => 'smile.png',
   ':clap:' => 'clap.png',
   ':envious:' => 'envious.png',
   ':surrender:' => 'surrender.png',
   ':bigeye:' => 'bigeye.png',
   ':nose:' => 'nose.png',
   ':sweat:' => 'sweat.png',
   ':flower:' => 'flower.png',
   ':crying:' => 'crying.png',
   ':spit:' => 'spit.png',
   ':surprised:' => 'surprised.png',
   ':blood:' => 'blood.png',
   ':blood2:' => 'blood2.png',
   ':shy2:' => 'shy2.png',
   ':angry:' => 'angry.png',
   ':nomatter:' => 'nomatter.png',
   ':dead:' => 'dead.png',
   ':insidious:' => 'insidious.png',
   ':tongue:' => 'tongue.png',
    );
}
smilies_reset();

 

到这里差不多就结束了,不过发现一个问题,就是表情图片在前段展示的特标小,查看源码发现所有的表情上都有height: 1em; max-height: 1em这么一个样式,全局搜索wordpress源码,发现在wp-includes/formatting.php 找到了相关代码,删除之后一切正常了~,不过这样一旦升级wordpress就完犊子了……

百度了一下,发现有大神给出了解决办法,这里粘贴复制下:

/**
 * 修复WordPress4.2表情尺寸异常问题
 * 代码转自(张戈博客):http://zhangge.net/5035.html
 * */
remove_action( 'comment_text',      'convert_smilies',   20 );  //移除4.2 表情相关钩子
remove_action( 'the_content',           'convert_smilies'   );  //移除4.2 表情相关钩子
remove_action( 'the_excerpt',            'convert_smilies'  );  //移除4.2 表情相关钩子
add_action( 'comment_text',     'convert_smilies_diy',    20);  //自定义表情相关钩子
add_action( 'the_content',          'convert_smilies_diy'   );  //自定义表情相关钩子
add_action( 'the_excerpt',      'convert_smilies_diy'       );  //自定义表情相关钩子
//原函数 convert_smilies 位于wp-includes/formatting.php
function convert_smilies_diy( $text ) {
  global $wp_smiliessearch;
  $output = '';
  if ( get_option( 'use_smilies' ) && ! empty( $wp_smiliessearch ) ) {
    // HTML loop taken from texturize function, could possible be consolidated
    $textarr = preg_split( '/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // capture the tags as well as in between
    $stop = count( $textarr );// loop stuff
    // Ignore proessing of specific tags
    $tags_to_ignore = 'code|pre|style|script|textarea';
    $ignore_block_element = '';
    for ( $i = 0; $i < $stop; $i++ ) {
      $content = $textarr[$i];
      // If we're in an ignore block, wait until we find its closing tag
      if ( '' == $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')>/', $content, $matches ) )  {
        $ignore_block_element = $matches[1];
      }
      // If it's not a tag and not in ignore block
      if ( '' ==  $ignore_block_element && strlen( $content ) > 0 && '<' != $content[0] ) {
        $content = preg_replace_callback( $wp_smiliessearch, 'translate_smiley_diy', $content );
      }
      // did we exit ignore block
      if ( '' != $ignore_block_element && '</' . $ignore_block_element . '>' == $content )  {
        $ignore_block_element = '';
      }
      $output .= $content;
    }
  } else {
    // return default text.
    $output = $text;
  }
  return $output;
}
//原函数 translate_smiley 位于wp-includes/formatting.php
function translate_smiley_diy( $matches ) {
  global $wpsmiliestrans;
  if ( count( $matches ) == 0 )
    return '';
  $smiley = trim( reset( $matches ) );
  $img = $wpsmiliestrans[ $smiley ];
  $matches = array();
  $ext = preg_match( '/\.([^.]+)$/', $img, $matches ) ? strtolower( $matches[1] ) : false;
  $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' );
  // Don't convert smilies that aren't images - they're probably emoji.
  if ( ! in_array( $ext, $image_exts ) ) {
    return $img;
  }
  //请注意!已将表情路径定义到主题目录下的 images/smilies 文件夹
  $src_url = apply_filters( 'smilies_src', get_bloginfo('template_directory').'/images/smilies/'.$img, $img, site_url() );
  return sprintf( '<img src="%s" alt="%s" class="wp-smiley" style="/*height: 1em; max-height: 1em;*/" />', esc_url( $src_url ), esc_attr( $smiley )         );
}

 

除非注明,否则均为李锋镝的博客原创文章,转载必须以链接形式标明本文链接

本文链接:https://www.lifengdi.com/archives/article/2589

相关文章

  • 写了个日期进度条的小插件
  • IDEA下载源码报:Cannot connect to the Maven process. Try again later.
  • 百度的索引量数据现在有点儿太夸张了吧?
  • 关于8月29号下午博客故障的一些记录
  • 网站升级到http/2
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: 博客 表情
最后更新:2021年3月14日

李锋镝

既然选择了远方,便只顾风雨兼程。

打赏 点赞
< 上一篇
下一篇 >

文章评论

  • 李锋镝

    :hehe:

    2021年4月15日
    回复
  • Catyo

    :huaji7: 针不戳

    2021年3月15日
    回复
    • 李锋镝

      @Catyo :huaji20:

      2021年3月15日
      回复
  • 强仔

    :huaji3: 很强

    2021年3月14日
    回复
    • 李锋镝

      @强仔 :huaji22: 哈哈~~都是粘贴复制~

      2021年3月15日
      回复
  • 1 2 3 4 5 6 7 8 9 11 12 13 14 15 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 46 47 48 49 50 51 52 53 54 55 57 58 60 61 62 63 64 65 66 67 69 72 74 76 77 78 79 80 81 82 85 86 87 90 92 93 94 95 96 97 98 99
    回复 Catyo 取消回复

    夜阑卧听风吹雨,铁马冰河入梦来。

    最新 热点 随机
    最新 热点 随机
    SpringBoot框架自动配置之spring.factories和AutoConfiguration.imports 应用型负载均衡(ALB)和网络型负载均衡(NLB)区别 什么是Helm? TransmittableThreadLocal介绍与使用 ReentrantLock深度解析 RedisTemplate和Redisson的区别
    玩博客的人是不是越来越少了?准备入手个亚太的ECS,友友们有什么建议吗?什么是Helm?2024年11月1号 农历十月初一别再背线程池的七大参数了,现在面试官都这么问URL地址末尾加不加“/”有什么区别
    Elasticsearch零停机迁移数据 感觉Typecho很简洁啊…… StarUML4.0破解文件 RedisTemplate和Redisson的区别 我要狠狠的反驳“公司禁止使用 Lombok ”的观点! 玉楼春·尊前拟把归期说
    标签聚合
    数据库 SQL 多线程 日常 JAVA 分布式 架构 SpringBoot IDEA 教程 JVM docker 面试 Spring ElasticSearch Redis 设计模式 MySQL 文学 K8s
    友情链接
    • i架构
    • 临窗旋墨
    • 博友圈
    • 博客录
    • 博客星球
    • 哥斯拉
    • 志文工作室
    • 搬砖日记
    • 旋律的博客
    • 旧时繁华
    • 林羽凡
    • 知向前端
    • 蜗牛工作室
    • 集博栈
    • 韩小韩博客
    • 風の声音

    COPYRIGHT © 2025 lifengdi.com. ALL RIGHTS RESERVED.

    Theme Kratos Made By Dylan

    津ICP备2024022503号-3