Skip to content

Instantly share code, notes, and snippets.

@westonruter
Created August 21, 2025 01:22
Show Gist options
  • Save westonruter/6b201c637e40d6d3b865e5cf7548c59e to your computer and use it in GitHub Desktop.
Save westonruter/6b201c637e40d6d3b865e5cf7548c59e to your computer and use it in GitHub Desktop.
<?php
/**
* Emoji Detection Script Deferred Plugin for WordPress
*
* @package EmojiDetectionScriptDeferred
* @author Weston Ruter
* @license GPL-2.0-or-later
*
* @wordpress-plugin
* Plugin Name: Emoji Detection Script Deferred
* Plugin URI: https://core.trac.wordpress.org/ticket/63842
* Description: Replaces inline blocking script with a JSON script for settings and script module(s) for the emoji detection logic. Polyfill to implement <a href="https://core.trac.wordpress.org/ticket/63842">#63842</a>.
* Requires at least: 6.8
* Requires PHP: 7.2
* Version: 0.1.0
* Author: Weston Ruter
* Author URI: https://weston.ruter.net/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Update URI: https://gist.github.com/westonruter/6b201c637e40d6d3b865e5cf7548c59e
* Gist Plugin URI: https://gist.github.com/westonruter/6b201c637e40d6d3b865e5cf7548c59e
* Primary Branch: main
*/
namespace EmojiDetectionScriptDeferred;
if ( isset( $_GET['emoji_detection_script_deferred_disabled'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return;
}
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
add_action( 'wp_head', __NAMESPACE__ . '\print_deferred_emoji_detection_script', 7 );
/**
* Prints deferred inline Emoji detection script.
*
* This is a forked version of `print_emoji_detection_script()` and `_print_emoji_detection_script()` in core.
*
* @see \print_emoji_detection_script()
* @see \_print_emoji_detection_script()
* @link https://github.com/WordPress/wordpress-develop/blob/450524a5a7a0708875f82ee5e75bb90c04049dbe/src/wp-includes/formatting.php#L6077-L6143
*/
function print_deferred_emoji_detection_script(): void {
static $printed = false;
if ( $printed ) {
return;
}
$printed = true;
$settings = array(
/**
* Filters the URL where emoji png images are hosted.
*
* @since 4.2.0
*
* @param string $url The emoji base URL for png images.
*/
'baseUrl' => apply_filters( 'emoji_url', 'https://s.w.org/images/core/emoji/16.0.1/72x72/' ),
/**
* Filters the extension of the emoji png files.
*
* @since 4.2.0
*
* @param string $extension The emoji extension for png files. Default .png.
*/
'ext' => apply_filters( 'emoji_ext', '.png' ),
/**
* Filters the URL where emoji SVG images are hosted.
*
* @since 4.6.0
*
* @param string $url The emoji base URL for svg images.
*/
'svgUrl' => apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/16.0.1/svg/' ),
/**
* Filters the extension of the emoji SVG files.
*
* @since 4.6.0
*
* @param string $extension The emoji extension for svg files. Default .svg.
*/
'svgExt' => apply_filters( 'emoji_svg_ext', '.svg' ),
);
$version = 'ver=' . get_bloginfo( 'version' );
if ( SCRIPT_DEBUG ) {
$settings['source'] = array(
/** This filter is documented in wp-includes/class-wp-scripts.php */
'wpemoji' => apply_filters( 'script_loader_src', includes_url( "js/wp-emoji.js?$version" ), 'wpemoji' ),
/** This filter is documented in wp-includes/class-wp-scripts.php */
'twemoji' => apply_filters( 'script_loader_src', includes_url( "js/twemoji.js?$version" ), 'twemoji' ),
);
} else {
$settings['source'] = array(
/** This filter is documented in wp-includes/class-wp-scripts.php */
'concatemoji' => apply_filters( 'script_loader_src', includes_url( "js/wp-emoji-release.min.js?$version" ), 'concatemoji' ),
);
}
// --------- BEGIN CORE PATCH ---------
wp_print_inline_script_tag(
wp_json_encode( $settings, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
array(
'id' => 'wp-emoji-settings',
'type' => 'application/json',
)
);
wp_print_inline_script_tag(
'window._wpemojiSettings = /** @type {WPEmojiSettings} */ (JSON.parse( document.getElementById( \'wp-emoji-settings\' ).textContent ) );',
array(
'type' => 'module',
)
);
wp_print_inline_script_tag(
file_get_contents( ABSPATH . WPINC . '/js/wp-emoji-loader' . wp_scripts_get_suffix() . '.js' ),
array(
'type' => 'module',
)
);
// --------- END CORE PATCH ---------
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment