如何禁用WordPress表情符號Emojis

禁用WordPress的表情符號Emojis

我們可以進行許多網路效能優化和調整,以使WordPress網站載入速度更快。其中一種簡單的優化方法是禁止載入表情符號Emojis。表情符號(Emojis)是用於表達想法或情感的小圖示。儘管這些圖示很有趣,但它們對於WordPress網站真的必要嗎?對於很多網站來說,或者這些只是增加了不必要的額外載入時間。

當WordPress 4.2釋出時,針對較舊版本的瀏覽器WordPress核心中新增了對錶情符號的支援。最大的問題是,它會在WordPress網站上生成一個額外的HTTP請求,以載入wp-emoji-release.min.js檔案。每個頁面都會載入此檔案,儘管此檔案只有10.5 KB,但隨著時間的推移,類似的東西會累加起來。

wp-emoji-release

Emojis表情符號JS檔案

大多數 WordPress 使用者並不需要 WordPress 的支援,它最終只會降低網站的執行速度,因此我們建議您禁用 WordPress 中的表情符號。

許多效能外掛(如 WPTurbo)也會為您提供禁用表情符號的選項。如果您目前沒有禁用表情符號,本指南將向您展示在 WordPress 中禁用表情符號的外掛。我還會向你展示在 functions.php 中禁用表情符號支援的程式碼片段。

有兩種不同的方法禁用WordPress表情符號Emojis:使用免費外掛或程式碼來實現。

1. 使用外掛禁用Emojis

禁用表情符號Emojis的第一種方法是簡單地使用由Ryan Hellyer開發的名為 Disable Emojis 的免費外掛。

disable-emojis-wordpress-plugin

Disable Emojis外掛

這是一個超輕量級的外掛,確切地說只有9 KB。該外掛目前擁有70,000多個啟用安裝,100%的好評5星。注意:Emoticons和emojis仍然可以在內建支援它們的瀏覽器中使用。該外掛僅刪除了多餘的JavaScript檔案,該檔案用於在較舊的瀏覽器中新增對錶情符號的支援。

還有一個名為Emoji settings的免費替代外掛。這是在考慮到多站點的情況下開發的,  併為使用者提供了禁用表情符號本身的選項。

emoji-settings

Emoji settings外掛

啟用外掛後,使用者可以在WordPress儀表板的“設定-撰寫”中選中或取消選中“Enable emoji support”。

您還可以使用perfmatters類的高階外掛,該外掛支援禁用emoji表情以及WordPress網站的其他優化功能。

disable-emojis-with-perfmatters

使用外掛perfmatters禁用emojis

還有閃電博開發的 WordPress 效能優化外掛 WPTurbo,也支援禁用emojis。且該外掛還支援一系列的WP瘦身、速度優化、資料庫優化和物件儲存等功能。

使用 WPTurbo 禁用 emoji

使用 WPTurbo 禁用 emoji

2. 修改程式碼禁用Emojis

如果您不想安裝各種雜七雜八的外掛,也可以使用程式碼禁用表情符號。只需將以下內容新增到WordPress主題的functions.php檔案中。這實質是Disable Emoji外掛的程式碼。

警告:修改程式碼前請先備份原始檔,以免因操作不當導致網站出現問題,可以通過備份恢復!
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/**
* 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_filter( 'wp_resource_hints', 'disable_emojis_remove_dns_prefetch', 10, 2 );
}
add_action( 'init', 'disable_emojis' );
/**
* Filter function used to remove the tinymce emoji plugin.
*
* @param array $plugins
* @return array Difference betwen the two arrays
*/
function disable_emojis_tinymce( $plugins ) {
if ( is_array( $plugins ) ) {
return array_diff( $plugins, array( 'wpemoji' ) );
} else {
return array();
}
}
/**
* Remove emoji CDN hostname from DNS prefetching hints.
*
* @param array $urls URLs to print for resource hints.
* @param string $relation_type The relation type the URLs are printed for.
* @return array Difference betwen the two arrays.
*/
function disable_emojis_remove_dns_prefetch( $urls, $relation_type ) {
if ( 'dns-prefetch' == $relation_type ) {
/** This filter is documented in wp-includes/formatting.php */
$emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' );
$urls = array_diff( $urls, array( $emoji_svg_url ) );
}
return $urls;
}
/** * 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_filter( 'wp_resource_hints', 'disable_emojis_remove_dns_prefetch', 10, 2 ); } add_action( 'init', 'disable_emojis' ); /** * Filter function used to remove the tinymce emoji plugin. * * @param array $plugins * @return array Difference betwen the two arrays */ function disable_emojis_tinymce( $plugins ) { if ( is_array( $plugins ) ) { return array_diff( $plugins, array( 'wpemoji' ) ); } else { return array(); } } /** * Remove emoji CDN hostname from DNS prefetching hints. * * @param array $urls URLs to print for resource hints. * @param string $relation_type The relation type the URLs are printed for. * @return array Difference betwen the two arrays. */ function disable_emojis_remove_dns_prefetch( $urls, $relation_type ) { if ( 'dns-prefetch' == $relation_type ) { /** This filter is documented in wp-includes/formatting.php */ $emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' ); $urls = array_diff( $urls, array( $emoji_svg_url ) ); } return $urls; }
/**
 * 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_filter( 'wp_resource_hints', 'disable_emojis_remove_dns_prefetch', 10, 2 );
}
add_action( 'init', 'disable_emojis' );

/**
 * Filter function used to remove the tinymce emoji plugin.
 * 
 * @param array $plugins 
 * @return array Difference betwen the two arrays
 */
function disable_emojis_tinymce( $plugins ) {
 if ( is_array( $plugins ) ) {
 return array_diff( $plugins, array( 'wpemoji' ) );
 } else {
 return array();
 }
}

/**
 * Remove emoji CDN hostname from DNS prefetching hints.
 *
 * @param array $urls URLs to print for resource hints.
 * @param string $relation_type The relation type the URLs are printed for.
 * @return array Difference betwen the two arrays.
 */
function disable_emojis_remove_dns_prefetch( $urls, $relation_type ) {
 if ( 'dns-prefetch' == $relation_type ) {
 /** This filter is documented in wp-includes/formatting.php */
 $emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' );

$urls = array_diff( $urls, array( $emoji_svg_url ) );
 }

return $urls;
}

就這樣,您就禁用了 WordPress 網站上的表情符號。雖然這只是提高網站效能的一小部分,但我強烈建議您這樣做。請務必檢視我們為 WordPress 加速的更多技巧

評論留言