Emoji support in WordPress, while adding a fun visual element, can also introduce unnecessary CSS and JavaScript files to your website's frontend. For most websites, especially blogs or business sites that don't heavily rely on emojis in their content, disabling this feature can offer a small but beneficial performance boost. This optimisation helps reduce HTTP requests and overall page weight, contributing to faster load times and potentially better SEO.

Here's how to disable emoji support in WordPress:

Adding Code to Your functions.php File

This is the most common and effective method. Add the following code to your theme's functions.php file. For better practice and to ensure your changes are not lost during theme updates, consider placing this code in a custom plugin or a child theme's functions.php file.

PHP
 
<?php
/**
 * Disable the emoji's styles and scripts.
 */
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 The list of URLs that should bePrefetched.
 * @param string $relation_type The relation type the URLs are prefetching for.
 * @return array The filtered list of URLs.
 */
function disable_emojis_remove_dns_prefetch( $urls, $relation_type ) {
    if ( 'dns-prefetch' == $relation_type ) {
        /**
         * Strip out any URLs relating to emojis
         */
        $emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/15.0.3/svg/' ); // Adjust version if needed
        $urls = array_diff( $urls, array( $emoji_svg_url ) );
    }
    return $urls;
}
?>

Explanation of the Code:

  • disable_emojis() function: This is the main function that handles the removal.

  • remove_action() calls: These lines remove the actions that enqueue the emoji-related JavaScript (print_emoji_detection_script) and CSS (print_emoji_styles) from both the frontend (wp_head, wp_print_styles) and the admin area (admin_print_scripts, admin_print_styles).

  • remove_filter() calls: These lines prevent WordPress from converting emoji characters into static images in feeds and emails.

  • add_filter( 'tiny_mce_plugins', 'disable_emojis_tinymce' ): This line targets the TinyMCE editor (used in the classic editor and as a component in Gutenberg) and removes its emoji plugin, preventing it from adding emoji-related features.

  • disable_emojis_tinymce() function: This helper function specifically removes the 'wpemoji' plugin from the TinyMCE editor's list of loaded plugins.

  • add_filter( 'wp_resource_hints', 'disable_emojis_remove_dns_prefetch', 10, 2 ): This line targets resource hints, specifically DNS prefetching.

  • disable_emojis_remove_dns_prefetch() function: This helper function removes the URL of the emoji CDN (s.w.org) from the DNS prefetch hints, further preventing any external requests related to emojis. The version number in the SVG URL (15.0.3 in the example) might need to be adjusted if WordPress updates its emoji set significantly in the future, but generally, this will still function correctly.

  • add_action( 'init', 'disable_emojis' ): This ensures that the disable_emojis function runs early in the WordPress loading process.

Using a Code Snippet Plugin

 

If you prefer not to edit your theme files directly, a code snippet plugin is an excellent alternative.

  1. Install and activate a plugin like "WPCode" or "Code Snippets."

  2. Create a new snippet within the plugin.

  3. Paste the entire code block from Method 1 into the snippet.

  4. Set the snippet type to PHP.

  5. Activate the snippet.

Using a Performance Optimization Plugin

Many popular WordPress performance plugins offer a built-in option to disable emojis with a simple toggle switch. If you already use a plugin like WP Rocket, Perfmatters, Asset CleanUp, or LiteSpeed Cache, check their settings for an option to "Disable Emojis" or "Remove Emoji Scripts." This is often the easiest solution for those already using such tools.

SEO and Performance Impact

Disabling emoji support on your website's frontend yields several performance benefits that can positively influence your SEO:

  • Reduced HTTP Requests: By preventing the emoji-related CSS and JavaScript files from loading, you decrease the number of requests your browser makes to the server. Fewer requests generally mean faster page loads.

  • Smaller Page Size: The emoji scripts and stylesheets, while small individually, contribute to the overall weight of your web pages. Removing them shaves off a few kilobytes, leading to a leaner page.

  • Improved Page Load Speed: The combined effect of fewer requests and smaller page size results in a faster loading website. Page load speed is a direct ranking factor for Google and significantly impacts user experience.

  • Better Core Web Vitals: Faster loading directly improves your Core Web Vitals scores, particularly Largest Contentful Paint (LCP) and First Input Delay (FID), both of which are critical SEO metrics.

  • Cleaner Code: Removing unnecessary scripts leads to cleaner, more streamlined HTML, which can be marginally beneficial for crawlability.

While the individual impact of disabling emoji support might be modest, it is a simple and effective optimization that contributes to a faster, more efficient website, ultimately benefiting both user experience and search engine visibility.

Bu cavab sizə kömək etdi? 0 istifadəçi bunu faydalı hesab edir (0 səs)