settings = Plugin::get_settings( 'performance' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); if( !empty( $this->settings['disable_emojis'] ) ) { add_action( 'init', [ $this, 'disable_emojis' ] ); } } /** * Modifies and returns the settings fields for the plugin. * * @param array $fields An array containing the existing fields configuration. * * @return array The updated fields array with the disable emojis configuration. */ public function adminease_settings_fields( array $fields ): array { $fields['performance']['fields'][] = [ 'type' => 'switch', 'id' => 'disable-emojis', 'name' => 'adminease[performance][disable_emojis]', 'value' => $this->settings['disable_emojis'] ?? '', 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input', 'label' => __( 'Disable Emojis', 'adminease' ), 'description' => __( 'Remove WordPress emoji detection scripts and styles to improve page load performance. WordPress loads emoji scripts on every page by default to convert emoji characters into images for browsers that don\'t support native emoji rendering. Modern browsers (Chrome 58+, Firefox 52+, Safari 10+, Edge 79+) all support native emoji rendering, making these scripts unnecessary for most users. Disabling emojis eliminates extra HTTP requests and reduces page weight, resulting in faster load times.', 'adminease' ), ]; return $fields; } /** * Removes all emoji-related functionality from WordPress. * Disables emoji detection scripts, TinyMCE plugins, DNS prefetch, and email conversion. * * @return void */ public function disable_emojis(): void { 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', [ $this, 'disable_tinymce_emojis' ] ); add_filter( 'wp_resource_hints', [ $this, 'disable_emoji_dns_prefetch' ], 10, 2 ); } /** * Removes the emoji plugin from TinyMCE editor. * * @param array $plugins An array of TinyMCE plugins. * * @return array The modified array with emoji plugin removed. */ public function disable_tinymce_emojis( array $plugins ): array { if( is_array( $plugins ) ) { return array_diff( $plugins, [ 'wpemoji' ] ); } return []; } /** * Removes emoji DNS prefetch from resource hints. * * @param array $urls An array of resource URLs. * @param string $relation_type The relation type of the resource hints. * * @return array The modified array with emoji DNS prefetch removed. */ public function disable_emoji_dns_prefetch( array $urls, string $relation_type ): array { if( 'dns-prefetch' === $relation_type ) { $emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound, PluginCheck.CodeAnalysis.Offloading.OffloadedContent -- Default value of core's own emoji_svg_url filter, used only to identify and remove the DNS-prefetch hint core already added; no resource is requested. $urls = array_diff( $urls, [ $emoji_svg_url ] ); } return $urls; } }