IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    WordPress禁用Emoji表情及本地化Emoji提高网站速度

    老左发表于 2015-08-23 08:19:37
    love 0

    对于普通的个人博客、网站应用WordPress程序其实已经足够使用,但是因为官方的维护和不断的完善,在添加和修正安全的同时,也会增加一些其他的升级元素。这就使得我们在使用WordPress的时候会发现即便程序是非常完美的,目前也没有其他多么优秀的CMS可以替代,唯独就是比较臃肿,甚至会加载一些外部的调用。

    尤其使得我们国内的用户访问比较慢,比如在WordPress4.2版本之后,增加了Emoji表情外部调用,大部分用户是使用不到的,我们如果认为也没有必要,可以依据下面的方法之一去掉或者替换本地加载。

    第一、检查是否有Emoji表情

    emoji检查

    如果我们查看源文件,可以看到上面的脚本,说明我们网站还是加载Emoji表情的,这里是调用外部文件的,我们要么选择禁用,要么选择文档本地化,这样可以提高速度。

    第二、禁用Emoji表情脚本

    禁用表情可以直接使用Disable Emojis插件,但是我们还是本着能少用插件就少用的原则,直接使用在当前主题的Functions.php文件中添加下面脚本禁用。

         /**
    * Disable the emoji's
    */
    function disable_emojis() {
    remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
    remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
    remove_action( 'wp_print_styles', 'print_emoji_styles' );
    remove_action( 'admin_print_styles', 'print_emoji_styles' );
    remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
    remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
    remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
    add_filter( 'tiny_mce_plugins', 'disable_emojis_tinymce' );
    }
    add_action( 'init', 'disable_emojis' );
    /**
    * Filter function used to remove the tinymce emoji plugin.
    */
    function disable_emojis_tinymce( $plugins ) {
    if ( is_array( $plugins ) ) {
    return array_diff( $plugins, array( 'wpemoji' ) );
    } else {
    return array();
    }
    }

    添加之后我们重新更新下博客的缓存就可以生效。

    第三、Emoji表情本地化

    或者,我们也可以采用imjeff的方法(www.imjeff.cn/blog/448/),将表情本地化,这样我们也可以使用表情(一般人是用不上的,如果你真用,那就本地化)

    1、下载表情放到当前主题下

    下载地址:http://soft.laozuo.org/wordpress/72x72.zip

    我们将表情文件夹放到当前主题目录下,文件夹名称不要变。

    2、将下面脚本放到当前主题下的Functions.php文件中

    //首先补全wp的表情库
    function smilies_reset() {
    global $wpsmiliestrans, $wp_smiliessearch;
    // don't bother setting up smilies if they are disabled
    if (!get_option('use_smilies')) {
    return;
    }
    $wpsmiliestrans_fixed = array(
    ':mrgreen:' => "\xf0\x9f\x98\xa2",
    ':smile:' => "\xf0\x9f\x98\xa3",
    ':roll:' => "\xf0\x9f\x98\xa4",
    ':sad:' => "\xf0\x9f\x98\xa6",
    ':arrow:' => "\xf0\x9f\x98\x83",
    ':-(' => "\xf0\x9f\x98\x82",
    ':-)' => "\xf0\x9f\x98\x81",
    ':(' => "\xf0\x9f\x98\xa7",
    ':)' => "\xf0\x9f\x98\xa8",
    ':?:' => "\xf0\x9f\x98\x84",
    ':!:' => "\xf0\x9f\x98\x85",
    );
    $wpsmiliestrans = array_merge($wpsmiliestrans, $wpsmiliestrans_fixed);
    }
    //替换cdn路径
    function static_emoji_url() {
    return get_bloginfo('template_directory').'/72x72/';
    }
    //让文章内容和评论支持 emoji 并禁用 emoji 加载的乱七八糟的脚本
    function reset_emojis() {
    remove_action('wp_head', 'print_emoji_detection_script', 7);
    remove_action('admin_print_scripts', 'print_emoji_detection_script');
    remove_action('wp_print_styles', 'print_emoji_styles');
    remove_action('admin_print_styles', 'print_emoji_styles');
    add_filter('the_content', 'wp_staticize_emoji');
    add_filter('comment_text', 'wp_staticize_emoji',50); //在转换为表情后再转为静态图片
    smilies_reset();
    add_filter('emoji_url', 'static_emoji_url');
    }
    add_action('init', 'reset_emojis');
    //输出表情
    function fa_get_wpsmiliestrans(){
    global $wpsmiliestrans;
    $wpsmilies = array_unique($wpsmiliestrans);
    foreach($wpsmilies as $alt => $src_path){
    $emoji = str_replace(array('&#x', ';'), '', wp_encode_emoji($src_path));
    $output .= '<a class="add-smily" data-smilies="'.$alt.'"><img class="wp-smiley" src="'.get_bloginfo('template_directory').'/72x72/'. $emoji .'png" /></a>';
    }
    return $output;
    }

    这样Emoji本地化就解决了。

    总结,老左个人建议还是禁用掉算了,一般也没多大用途。



沪ICP备19023445号-2号
友情链接