WordPress 教程:WordPress 4.2后 头部多出的Emoji表情的处理方法

如果你更新到 wordpress 的 4.2 版本,查看网页源代码你会发现 WordPress 会自动在加载一段用于支持 emjo 表情的脚本(JS+CSS)。对于大部分人来说,这个是十分鸡肋的功能,再加上 GFW 的强大力量,反而影响加载速度。

我们有两种解决方法:启用或禁用。

原因分析

脚本就是类似下面的代码:

  1. <script type="text/javascript">
  2.  window._wpemojiSettings = {"baseUrl":"http:////s.w.org//images//core//emoji//72x72//","ext":".png","source":{"concatemoji":"http:////devework.com//wp-includes//js//wp-emoji-release.min.js?ver=4.2"}};
  3.  !function(a,b,c){function d(a){var c=b.createElement("canvas"),d=c.getContext&&c.getContext("2d");return d&&d.fillText?(d.textBaseline="top",d.font="600 32px Arial","flag"===a?(d.fillText(String.fromCharCode(55356,56812,55356,56807),0,0),c.toDataURL().length>3e3):(d.fillText(String.fromCharCode(55357,56835),0,0),0!==d.getImageData(16,16,1,1).data[0])):!1}function e(a){var c=b.createElement("script");c.src=a,c.type="text/javascript",b.getElementsByTagName("head")[0].appendChild(c)}var f;c.supports={simple:d("simple"),flag:d("flag")},c.supports.simple&&c.supports.flag||(f=c.source||{},f.concatemoji?e(f.concatemoji):f.wpemoji&&f.twemoji&&(e(f.twemoji),e(f.wpemoji)))}(window,document,window._wpemojiSettings);
  4.  </script>
  5.  <style type="text/css">
  6.  img.wp-smiley,
  7.  img.emoji {
  8.  display: inline !important;
  9.  border: none !important;
  10.  box-shadow: none !important;
  11.  height: 1em !important;
  12.  width: 1em !important;
  13.  margin: 0 .07em !important;
  14.  vertical-align: -0.1em !important;
  15.  background: none !important;
  16.  padding: 0 !important;
  17.  }
  18.  </style>

因为WordPress 更新 4.2 的一个新增功能就是支持 emjo 表情,但看部分加载源居然是 wp.org 的 js 文件。对于大部分人来说,这个是十分鸡肋的功能。

禁用:移除 WordPress 4.2 中前台自动加载的 emoji 脚本

既然功能鸡肋,不如直接移除掉来得更加快捷。代码提取自 Disable Emojis 插件,可以放在主题目录下的 functions.php 文件中:

  1. /**
  2.  * Disable the emoji's
  3.  */
  4.  function disable_emojis() {
  5.  remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
  6.  remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
  7.  remove_action( 'wp_print_styles', 'print_emoji_styles' );
  8.  remove_action( 'admin_print_styles', 'print_emoji_styles' );
  9.  remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
  10.  remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
  11.  remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
  12.  add_filter( 'tiny_mce_plugins', 'disable_emojis_tinymce' );
  13.  }
  14.  add_action( 'init', 'disable_emojis' );
  15. /**
  16.  * Filter function used to remove the tinymce emoji plugin.
  17.  */
  18.  function disable_emojis_tinymce( $plugins ) {
  19.  if ( is_array$plugins ) ) {
  20.  return array_diff$pluginsarray( 'wpemoji' ) );
  21.  } else {
  22.  return array();
  23.  }
  24.  }

启用:转存至本地调用 Emoji 表情

WordPress 官方将此功能会写入正式版一定有其理由。但我们知道 WP 的 CDN 早就被*掉,唯一方法就是转存到本地,使 WP 识别本地 Emoji 表情。

Twitter Emoji 表情包下载,下载后直接解压至主题目录,文件夹名不变。将以下代码放在主题目录下的 functions.php 文件中:

  1. //首先补全wp的表情库  
  2.  function smilies_reset() {  
  3.  global $wpsmiliestrans$wp_smiliessearch;  
  4.  // don't bother setting up smilies if they are disabled  
  5.  if (!get_option('use_smilies')) {  
  6.  return;  
  7.  }  
  8.  $wpsmiliestrans_fixed = array(  
  9.  ':mrgreen:' => "/xf0/x9f/x98/xa2",  
  10.  ':smile:' => "/xf0/x9f/x98/xa3",  
  11.  ':roll:' => "/xf0/x9f/x98/xa4",  
  12.  ':sad:' => "/xf0/x9f/x98/xa6",  
  13.  ':arrow:' => "/xf0/x9f/x98/x83",  
  14.  ':-(' => "/xf0/x9f/x98/x82",  
  15.  ':-)' => "/xf0/x9f/x98/x81",  
  16.  ':(' => "/xf0/x9f/x98/xa7",  
  17.  ':)' => "/xf0/x9f/x98/xa8",  
  18.  ':?:' => "/xf0/x9f/x98/x84",  
  19.  ':!:' => "/xf0/x9f/x98/x85",  
  20.  );  
  21.  $wpsmiliestrans = array_merge($wpsmiliestrans$wpsmiliestrans_fixed);  
  22.  }  
  23.  //替换cdn路径  
  24.  function static_emoji_url() {  
  25.  return get_bloginfo('template_directory').'/72x72/';  
  26.  }  
  27.  //让文章内容和评论支持 emoji 并禁用 emoji 加载的乱七八糟的脚本  
  28.  function reset_emojis() {  
  29.  remove_action('wp_head', 'print_emoji_detection_script', 7);  
  30.  remove_action('admin_print_scripts', 'print_emoji_detection_script');  
  31.  remove_action('wp_print_styles', 'print_emoji_styles');  
  32.  remove_action('admin_print_styles', 'print_emoji_styles');  
  33.  add_filter('the_content', 'wp_staticize_emoji');  
  34.  add_filter('comment_text', 'wp_staticize_emoji',50); //在转换为表情后再转为静态图片  
  35.  smilies_reset();  
  36.  add_filter('emoji_url', 'static_emoji_url');  
  37.  }  
  38.  add_action('init', 'reset_emojis');  
  39.  //输出表情  
  40.  function fa_get_wpsmiliestrans(){  
  41.  global $wpsmiliestrans;  
  42.  $wpsmilies = array_unique($wpsmiliestrans);  
  43.  foreach($wpsmilies as $alt => $src_path){  
  44.  $emoji = str_replace(array('&#x', ';'), '', wp_encode_emoji($src_path));  
  45.  $output .= '<a class="add-smily" data-smilies="'.$alt.'"><img class="wp-smiley" src="'.get_bloginfo('template_directory').'/72x72/'. $emoji .'png" /></a>';  
  46.  }  
  47.  return $output;  
  48.  }  

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如果您也有好的资源或教程,您可以投稿发布,成功分享后有M币奖励和额外收入!

模板下载吧 快速入门 WordPress 教程:WordPress 4.2后 头部多出的Emoji表情的处理方法 https://www.mbxzb.com/blog/file/rumen/3179.html

从明天起,做一个幸福的人,喂马、劈柴、周游世界…

常见问题
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用
查看详情

相关文章

评论
暂无评论
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 威武 友军 顶贴
官方客服团队

为您解决烦忧 - 24小时在线 专业服务