{"id":120895,"date":"2020-04-29T20:03:13","date_gmt":"2020-04-29T20:03:13","guid":{"rendered":"https:\/\/wordpress.org\/plugins\/embed-javascript-file-content\/"},"modified":"2026-08-03T15:44:39","modified_gmt":"2026-08-03T15:44:39","slug":"embed-javascript-file-content","status":"closed","type":"plugin","link":"https:\/\/ug.wordpress.org\/plugins\/embed-javascript-file-content\/","author":14867054,"comment_status":"closed","ping_status":"closed","template":"","meta":{"version":"1.0.2","stable_tag":"1.0.2","tested":"7.0.4","requires":"4.1","requires_php":"5.4","requires_plugins":null,"header_name":"Embed JavaScript File Content","header_author":"Palasthotel <rezeption@palasthotel.de> (Kim Meyer)","header_description":"Boosts performance of critical short JavaScript files by embedding their code instead of linking to files. Script positions and extra scripts are preserved.","assets_banners_color":"","last_updated":"2026-08-03 15:44:39","external_support_url":"","external_repository_url":"","donate_link":"https:\/\/palasthotel.de\/","header_plugin_uri":"","header_author_uri":"https:\/\/palasthotel.de","rating":0,"author_block_rating":0,"active_installs":10,"downloads":1253,"num_ratings":0,"support_threads":0,"support_threads_resolved":0,"author_block_count":0,"sections":["description","installation","changelog"],"tags":{"1.0":{"tag":"1.0","author":"palasthotel","date":"2020-04-29 20:03:39"},"1.0.2":{"tag":"1.0.2","author":"palasthotel","date":"2026-08-03 15:44:39"}},"upgrade_notice":{"1.0.2":"<p>This plugin is retired. This release repairs a fatal error on PHP 8 and carries the\nretirement notice. Please deactivate and remove it; the description explains what\nto use instead.<\/p>"},"ratings":[],"assets_icons":[],"assets_banners":[],"assets_blueprints":{},"all_blocks":[],"tagged_versions":["1.0","1.0.2"],"block_files":[],"assets_screenshots":[],"screenshots":[]},"plugin_section":[],"plugin_tags":[230,23414,3936,229,2864],"plugin_category":[56,59],"plugin_contributors":[138993,138992],"plugin_business_model":[],"class_list":["post-120895","plugin","type-plugin","status-closed","hentry","plugin_tags-embed","plugin_tags-enqueue","plugin_tags-inline","plugin_tags-javascript","plugin_tags-scripts","plugin_category-social-and-sharing","plugin_category-utilities-and-tools","plugin_contributors-greatestview","plugin_contributors-palasthotel","plugin_committers-greatestview","plugin_committers-palasthotel"],"banners":[],"icons":{"svg":false,"icon":"https:\/\/s.w.org\/plugins\/geopattern-icon\/embed-javascript-file-content.svg","icon_2x":false,"generated":true},"screenshots":[],"raw_content":"<!--section=description-->\n<p><strong>This plugin is retired and no longer maintained. Please deactivate and remove it.<\/strong><\/p>\n\n<p>Version 1.0.2 exists only to carry this notice and to repair a fatal error: the\nplugin called <code>DOMDocument::loadHTML()<\/code> statically, which PHP 8 rejects. Any site\nthat actually configured the filter has been showing \"There has been a critical\nerror on this website\" since its PHP 8 upgrade. Sites that installed the plugin\nwithout configuring the filter were unaffected, because it does nothing until a\nhandle is passed to it.<\/p>\n\n<h4>What to do instead<\/h4>\n\n<p>If you wrote the script yourself, do not inline the file \u2014 write the code inline\nin the first place. WordPress has supported that since 4.5:<\/p>\n\n<pre><code>wp_add_inline_script( 'my-handle', 'if (!(\"localStorage\" in window)) { \/* ... *\/ }', 'before' );\n<\/code><\/pre>\n\n<p>or, for code that has to run before anything else paints, print it in <code>wp_head<\/code>\ndirectly. Both are simpler than routing a file through a plugin, and neither\nbreaks a Content Security Policy the way an inlined file does \u2014 this plugin sets\nneither a nonce nor a hash, so a strict <code>script-src<\/code> blocks its output.<\/p>\n\n<p>For a script you do not control there is no drop-in replacement, and our older\nInline JavaScript in Head plugin is not one either \u2014 it was deprecated in 2020 in\nfavour of this plugin, so both are dead ends now. What is left is to dequeue the\nforeign script yourself and re-add its content inline:<\/p>\n\n<pre><code>add_action( 'wp_enqueue_scripts', function () {\n    $handle = 'some-foreign-handle';\n    $src    = wp_scripts()-&gt;registered[ $handle ]-&gt;src ?? null;\n    if ( ! $src ) {\n        return;\n    }\n    $path = ABSPATH . ltrim( wp_make_link_relative( $src ), '\/' );\n    if ( ! is_readable( $path ) ) {\n        return;\n    }\n    wp_dequeue_script( $handle );\n    wp_add_inline_script( 'some-handle-you-own', file_get_contents( $path ) );\n}, 20 );\n<\/code><\/pre>\n\n<p>That is roughly what this plugin did, minus the URL-to-path guessing \u2014 you decide\nhow the file is located, so it cannot break on a subdirectory install or a CDN.<\/p>\n\n<h4>Why it is being retired<\/h4>\n\n<p>The benefit it was written for has largely gone. Under HTTP\/2 and HTTP\/3 requests\nare multiplexed, so the per-file cost that made inlining worthwhile is mostly\ngone, and inlining still costs you browser caching \u2014 the code travels with every\nsingle HTML response. What remains is a narrow case: inlining a file you cannot\nedit. That is not enough to keep a plugin for.<\/p>\n\n<h4>How it used to work<\/h4>\n\n<p>In some critical cases you cannot wait for a JavaScript file to load. Then you can benefit from better performance, if you embed the JavaScript code directly into the <code>&lt;script&gt;<\/code> tag. This is where this plugin comes in: It provides a filter <code>embed_javascript_file_content_handles<\/code>, which takes JavaScript handles and echos their code content into the DOM instead of linking to a file.<\/p>\n\n<p>Please beware that placing lots of embedded JavaScript code can be critical! First you lose caching benefits and second the document size can increase easily. A general rule of thumb is that you should only consider JavaScript files for inline placement, which are critical and which have a file size lower than ~500 Bytes.<\/p>\n\n<h4>Example<\/h4>\n\n<pre><code>add_action( 'wp_enqueue_scripts', 'my_scripts' );\nfunction my_scripts() {\n    \/\/ Some critical script is enqueued\n    wp_enqueue_script( 'js-detection', get_template_directory_uri() . '\/js\/js-detection.js' );\n}\n\n\/**\n * Define JavaScript handles to be echoed inline in the html head section.\n *\/\nadd_filter( 'embed_javascript_file_content_handles', 'my_embed_javascript_file_content_handles' );\nfunction my_embed_javascript_file_content_handles( $handles ) {\n    $scripts = [ 'js-detection' ];\n\n    return array_merge( $handles, $scripts );\n}\n<\/code><\/pre>\n\n<!--section=installation-->\n<p>Please do not install this plugin any more \u2014 it is retired. See the description\nfor what to use instead.<\/p>\n\n<p>To remove it: drop the <code>embed_javascript_file_content_handles<\/code> filter from your\ntheme or plugin, then deactivate and delete the plugin. It stores no options and\ncreates no database tables, so nothing is left behind.<\/p>\n\n<!--section=changelog-->\n<h4>1.0.2<\/h4>\n\n<ul>\n<li>Final release. The plugin is retired and no longer maintained. Fixes a fatal error on PHP 8, where DOMDocument::loadHTML() was called statically. The readme explains what to use instead.<\/li>\n<\/ul>\n\n<h4>1.0.1<\/h4>\n\n<ul>\n<li>Small stabilization fix<\/li>\n<\/ul>\n\n<h4>1.0<\/h4>\n\n<ul>\n<li>First release<\/li>\n<\/ul>","raw_excerpt":"RETIRED \u2014 no longer maintained. Please deactivate and remove this plugin; the description explains what to use instead.","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ug.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin\/120895","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ug.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin"}],"about":[{"href":"https:\/\/ug.wordpress.org\/plugins\/wp-json\/wp\/v2\/types\/plugin"}],"replies":[{"embeddable":true,"href":"https:\/\/ug.wordpress.org\/plugins\/wp-json\/wp\/v2\/comments?post=120895"}],"author":[{"embeddable":true,"href":"https:\/\/ug.wordpress.org\/plugins\/wp-json\/wporg\/v1\/users\/palasthotel"}],"wp:attachment":[{"href":"https:\/\/ug.wordpress.org\/plugins\/wp-json\/wp\/v2\/media?parent=120895"}],"wp:term":[{"taxonomy":"plugin_section","embeddable":true,"href":"https:\/\/ug.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_section?post=120895"},{"taxonomy":"plugin_tags","embeddable":true,"href":"https:\/\/ug.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_tags?post=120895"},{"taxonomy":"plugin_category","embeddable":true,"href":"https:\/\/ug.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_category?post=120895"},{"taxonomy":"plugin_contributors","embeddable":true,"href":"https:\/\/ug.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_contributors?post=120895"},{"taxonomy":"plugin_business_model","embeddable":true,"href":"https:\/\/ug.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_business_model?post=120895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}