extension that inject tag to header without any additional config

Scarlett: 20 May 2022

I'm writing an extension for the community and I need it to inject an html tag to the head, which is not constant, eg:

<script>var something = "this value comes from php code";</script>

what's the best way to inject such a tag, to all site pages, without require the admin to do any additional config on the admin panel?



Based on Bernhard Prange's answer, here's the solution for future reference (note - it just do alert don't actually inject any meaningful javascript, but you can easily put anything in the template):




<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
        <reference name="head">

                <block type="page/html" name="dynamicJs" as="dynamicJs" template="page/html/inject_js.phtml" />



  <!-- note, DON'T put this inside the <global> tag! -->

Joshua: 20 May 2022

  • make a .phtml file it which you put the content above.
  • add the phtml file in you extension layout in the defaultblock with reference name="head"

open a File in app/design/frontend/base/default/layout/myExtensionLayout.xml

<layout version="0.1.0">    
      <default translate="label" module="page">
            <reference name="head">
              <block type="core/template" name="dynamicJs" as="dynamicJs" template="page/html/dynamicJs.phtml" />

Within app/design/frontend/base/default/template/page/html/dynamicJs.phtml you do you thing. And add this file with it's folder structure to your extension.

Sorry to be so short but I have to walk the dog.