{"id":98950,"date":"2019-03-11T13:33:03","date_gmt":"2019-03-11T13:33:03","guid":{"rendered":"https:\/\/wordpress.org\/plugins\/inline-javascript-in-head\/"},"modified":"2026-08-04T07:02:05","modified_gmt":"2026-08-04T07:02:05","slug":"inline-javascript-in-head","status":"closed","type":"plugin","link":"https:\/\/ta-lk.wordpress.org\/plugins\/inline-javascript-in-head\/","author":14867054,"comment_status":"closed","ping_status":"closed","template":"","meta":{"version":"1.2.1","stable_tag":"1.2.1","tested":"7.0.4","requires":"4.0","requires_php":"5.4","requires_plugins":null,"header_name":"Inline JavaScript in Head","header_author":"Palasthotel <rezeption@palasthotel.de> (Kim Meyer)","header_description":"Removes given local enqueued script handles and places their contents into the head. Useful for small scripts with a size lower than ~500 Bytes to boost site performance. Caution: Use with care and sparingly!","assets_banners_color":"","last_updated":"2026-08-04 07:02:05","external_support_url":"","external_repository_url":"","donate_link":"https:\/\/palasthotel.de\/","header_plugin_uri":"","header_author_uri":"https:\/\/palasthotel.de","rating":1,"author_block_rating":0,"active_installs":10,"downloads":1758,"num_ratings":1,"support_threads":0,"support_threads_resolved":0,"author_block_count":0,"sections":["description","installation","changelog"],"tags":{"1.1":{"tag":"1.1","author":"palasthotel","date":"2019-03-11 13:33:59"},"1.1.1":{"tag":"1.1.1","author":"palasthotel","date":"2019-03-11 15:55:34"},"1.1.2":{"tag":"1.1.2","author":"palasthotel","date":"2019-03-11 16:03:11"},"1.2.0":{"tag":"1.2.0","author":"palasthotel","date":"2020-04-29 20:04:55"},"1.2.1":{"tag":"1.2.1","author":"palasthotel","date":"2026-08-04 07:02:05"}},"upgrade_notice":{"1.2.1":"<p>This plugin is retired, and the successor this readme named since 2020 has been\nretired too. The description now explains what to use instead. Please deactivate and\nremove the plugin.<\/p>"},"ratings":{"1":"1","2":0,"3":0,"4":0,"5":0},"assets_icons":[],"assets_banners":[],"assets_blueprints":{},"all_blocks":[],"tagged_versions":["1.1","1.1.1","1.1.2","1.2.0","1.2.1"],"block_files":[],"assets_screenshots":[],"screenshots":[]},"plugin_section":[],"plugin_tags":[23414,3195,3936,229,2864],"plugin_category":[54,59],"plugin_contributors":[138993,274076,138992],"plugin_business_model":[],"class_list":["post-98950","plugin","type-plugin","status-closed","hentry","plugin_tags-enqueue","plugin_tags-head","plugin_tags-inline","plugin_tags-javascript","plugin_tags-scripts","plugin_category-security-and-spam-protection","plugin_category-utilities-and-tools","plugin_contributors-greatestview","plugin_contributors-janaeggebrecht","plugin_contributors-palasthotel","plugin_committers-greatestview","plugin_committers-palasthotel"],"banners":[],"icons":{"svg":false,"icon":"https:\/\/s.w.org\/plugins\/geopattern-icon\/inline-javascript-in-head.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>Since 2020 this readme pointed at <em>Embed JavaScript File Content<\/em> as the successor.\nThat plugin has now been retired as well, so this notice was sending you to a dead\nend. There is no successor plugin \u2014 here is what to do instead.<\/p>\n\n<h4>What to do instead<\/h4>\n\n<p>If you wrote the script yourself, do not route a file through a plugin \u2014 write the\ncode inline in 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>For code that has to run before anything paints, print it in <code>wp_head<\/code> directly.\nBoth are simpler, and neither breaks a Content Security Policy the way an inlined\nfile does \u2014 this plugin sets neither a nonce nor a hash, so a strict <code>script-src<\/code>\nblocks its output.<\/p>\n\n<p>For a script you do not control, dequeue it and re-add its content yourself:<\/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>You decide there how the file is located, which is the part this plugin got wrong:\nit resolved the path relative to the current working directory, so the result\ndepended on which entry script served the request.<\/p>\n\n<h4>Why it is being retired<\/h4>\n\n<p>The benefit it was written for has largely gone. Under HTTP\/1.1 every file cost a\nround trip, so inlining a small critical script was a real win. HTTP\/2 and HTTP\/3\nmultiplex requests and removed most of that cost, while inlining still gives up\nbrowser caching \u2014 the code travels with every HTML response instead of being cached\nonce.<\/p>\n\n<h4>How it used to work<\/h4>\n\n<p>In some cases you cannot wait for a JavaScript file to load, even if it is placed early in the <code>&lt;head&gt;<\/code> section of your template. You can benefit from better performance, if you place the JavaScript code directly inside a <code>&lt;script&gt;<\/code> tag into the header. This is where this plugin comes in: It provides a filter <code>inline_javascript_in_head_handles<\/code>, which takes JavaScript handles, dequeues those scripts and echos their code content inline into the head section instead of linking them via a script tag.<\/p>\n\n<p>Please beware that placing lots of JavaScript code inline in the <code>&lt;head&gt;<\/code> section 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( 'inline_javascript_in_head_handles', 'my_inline_javascript_in_head_handles', -20 );\nfunction my_inline_javascript_in_head_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 for\nwhat to use instead.<\/p>\n\n<p>To remove it: drop the <code>inline_javascript_in_head_handles<\/code> filter from your theme or\nplugin, then deactivate and delete the plugin. It stores no options and creates no\ndatabase tables, so nothing is left behind.<\/p>\n\n<!--section=changelog-->\n<h4>1.2.1<\/h4>\n\n<ul>\n<li>Final release. The plugin is retired. The successor named here since 2020, Embed JavaScript File Content, has been retired as well, so the readme now explains what to use instead rather than pointing at another deprecated plugin.<\/li>\n<\/ul>\n\n<h4>1.2.0<\/h4>\n\n<ul>\n<li>CAUTION: Last update! This plugin is now deprecated (see description section)<\/li>\n<li>Bugfix: Some scripts could have gotten lost under certain conditions.<\/li>\n<\/ul>\n\n<h4>1.1.2<\/h4>\n\n<ul>\n<li>readme.txt code appearance screwed up, now hopefully fixed.<\/li>\n<\/ul>\n\n<h4>1.1.1<\/h4>\n\n<ul>\n<li>readme.txt update<\/li>\n<\/ul>\n\n<h4>1.1<\/h4>\n\n<ul>\n<li>Added filter <code>inline_javascript_in_head_wrap_try_catch<\/code>, which can add add a try catch wrapper around the JavaScript code.<\/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:\/\/ta-lk.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin\/98950","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ta-lk.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin"}],"about":[{"href":"https:\/\/ta-lk.wordpress.org\/plugins\/wp-json\/wp\/v2\/types\/plugin"}],"replies":[{"embeddable":true,"href":"https:\/\/ta-lk.wordpress.org\/plugins\/wp-json\/wp\/v2\/comments?post=98950"}],"author":[{"embeddable":true,"href":"https:\/\/ta-lk.wordpress.org\/plugins\/wp-json\/wporg\/v1\/users\/palasthotel"}],"wp:attachment":[{"href":"https:\/\/ta-lk.wordpress.org\/plugins\/wp-json\/wp\/v2\/media?parent=98950"}],"wp:term":[{"taxonomy":"plugin_section","embeddable":true,"href":"https:\/\/ta-lk.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_section?post=98950"},{"taxonomy":"plugin_tags","embeddable":true,"href":"https:\/\/ta-lk.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_tags?post=98950"},{"taxonomy":"plugin_category","embeddable":true,"href":"https:\/\/ta-lk.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_category?post=98950"},{"taxonomy":"plugin_contributors","embeddable":true,"href":"https:\/\/ta-lk.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_contributors?post=98950"},{"taxonomy":"plugin_business_model","embeddable":true,"href":"https:\/\/ta-lk.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_business_model?post=98950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}