How to add block on every page in admin or front

Sometimes you might want to add block or javascript everywhere to the administration or everywhere to your theme. With Magento, this is very simple. What we need to do is to create a block and then extend layout separately for admin frontend. Also, we will need 2 separate templates.

Admin

Let’s start with Admin part first. We will create a custom module Magentoguides_JavascriptEverywhere.
Module config file with the path app\code\local\Magentoguides\JavascriptEverywhere\etc\config.xml
will have the following structure:

<?xml version="1.0"?>
<config>
    <modules>
        <Magentoguides_JavascriptEverywhere>
            <version>0.1.0</version>
        </Magentoguides_JavascriptEverywhere>
    </modules>
    <frontend>
        <layout>
            <updates>
                <javascripteverywhere>
                    <file>magentoguides_javascripteverywhere.xml</file> <!-- This is the layout file for Front -->
                </javascripteverywhere>
            </updates>
        </layout>
    </frontend>
    <adminhtml>
        <layout>
            <updates>
                <module>
                    <file>magentoguides_javascripteverywhere.xml</file> <!-- This is the layout file for Admin -->
                </module>
            </updates>
        </layout>
    </adminhtml>
    <global>
        <models>
            <magentoguides_javascripteverywhere>
                <class>Magentoguides_JavascriptEverywhere_Model</class>
                <resourceModel>magentoguides_javascripteverywhere_resource</resourceModel>
            </magentoguides_javascripteverywhere>
            <magentoguides_javascripteverywhere_resource>
                <class>Magentoguides_JavascriptEverywhere_Model_Resource</class>
            </magentoguides_javascripteverywhere_resource>
        </models>
        <blocks>
            <magentoguides_javascripteverywhere>
                <class>Magentoguides_JavascriptEverywhere_Block</class>
            </magentoguides_javascripteverywhere>
        </blocks>
        <helpers>
            <magentoguides_javascripteverywhere>
                <class>Magentoguides_JavascriptEverywhere_Helper</class>
            </magentoguides_javascripteverywhere>
        </helpers>
    </global>
</config>

Next step is to define a block at  app\code\local\Magentoguides\JavascriptEverywhere\Block\Adminhtml\EverywhereBlock.php

<?php

class Magentoguides_JavascriptEverywhere_Block_Adminhtml_EverywhereBlock extends Mage_Core_Block_Template
{

}

 

Then we will create the layout file which we defined in the config.xml app\design\adminhtml\default\default\layout\magentoguides_javascripteverywhere.xml

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="content">
            <block type="magentoguides_javascripteverywhere/adminhtml_everywhereBlock" name="securitycheck" template="magentoguides_javascripteverywhere/jseverywhere.phtml"/>
        </reference>
    </default>
</layout>

And lastly the template file app\design\adminhtml\default\default\template\magentoguides_javascripteverywhere\jseverywhere.phtml

<b>This html code will be shown everywhere!</b>
<script>
    alert('This javascript will be executed everywhere!');
</script>

 

Front

For the Front, we have to create same layout update file and same template file, however, in different directory since files for front and admin are separated.

Layout update file app\design\frontend\base\default\layout\magentoguides_javascripteverywhere.xml

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="content">
            <block type="magentoguides_javascripteverywhere/adminhtml_everywhereBlock" name="securitycheck" template="magentoguides/javascripteverywhere/myfile.phtml"/>
        </reference>
    </default>
</layout>

Template file with same content as the one we created above app\design\frontend\base\default\template\magentoguides\javascripteverywhere\myfile.phtml

<b>This html code will be shown everywhere!</b>
<script>
    alert('This javascript will be executed everywhere!');
</script>

 

Download module


Lukas
Lukas Stranovsky
I am the guy who made this. Sorry for typos.