Script on Every page of magento 2

Script For Every Page on Magento 2 Store

Some times we need to add custom script on every page of Magento 2  Ecommerce store. Its basically not complex task. We have built a simple and precise extension to achieve this.

 

Overview Of Code.

 

following is the list of files with code snippet.

  1. Expertdeveloper/EmbedScript/etc/module.xml


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Expertdeveloper_EmbedScript" setup_version="0.0.1" />
</config>

  1. Expertdeveloper/EmbedScript/composer.json


{
"name": "expertdeveloper/embedscript",
"description": "EmbedScript for Magento 2",
"type": "magento2-module",
"version": "0.0.1",
"authors": [
{
"name": "Expertdeveloper",
"email": "adeel729@gmail.com",
"homepage": "http://www.magento2extension.com/"
}
],
"license": [
"OSL-3.0",
"AFL-3.0"
],
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0",
"magento/magento-composer-installer": "*",
"magento/framework": "~100.0"
},
"autoload": {
"files": [ "registration.php" ],
"psr-4": {
"Expertdeveloper\\EmbedScript\\": ""
}
}
}

  1. Expertdeveloper/EmbedScript/registration.php


<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Expertdeveloper_EmbedScript',
__DIR__
);

 

  1. Expertdeveloper/EmbedScript/etc/frontend/routes.xml


<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="embedscript" frontName="embedscript">
<module name="Expertdeveloper_EmbedScript"/>
</route>
</router>
</config>

  1. Expertdeveloper/EmbedScript/etc/frontend/system.xml


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Expertdeveloper_EmbedScript:etc/system_file.xsd">
<system>
<tab id="embedscript" translate="label" sortOrder="10">
<label>Expert Developer</label>
</tab>
<section id="embedscript" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
<class>separator-top</class>
<label>Embed Script</label>
<tab>embedscript</tab>
<resource>Expertdeveloper_EmbedScript::hello_configuration</resource>
<group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
<label>General Configuration</label>
<field id="display_text" translate="label" type="textarea" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Additional Script</label>
<comment>This will add analytic code on the store frontend.</comment>
</field>
</group>
</section>
</system>
</config>

  1. Expertdeveloper/EmbedScript/Block/EmbedScript.php


<?php
namespace Expertdeveloper\EmbedScript\Block;
class EmbedScript extends \Magento\Framework\View\Element\Template
{
protected $_scopeConfig;

public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,

array $data = []
) {
$this->_scopeConfig = $scopeConfig;
parent::__construct($context, $data);
}

public function EmbedScript()
{
$script = $this->_scopeConfig->getValue(
’embedscript/general/display_text’,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);;
return $script;
}
}

  1. Expertdeveloper/EmbedScript/view/frontend/templates/embedscript.phtml

<?php
$script = $block->EmbedScript();
echo $script;
?>

  1. Expertdeveloper/EmbedScript/view/frontend/layout/default.xml


<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="after.body.start">
<block class="Expertdeveloper\EmbedScript\Block\EmbedScript" name="expertdeveloper.embedscript" as="expertdeveloper.embedscript" template="Expertdeveloper_EmbedScript::embedscript.phtml" />
</referenceContainer>
</body>
</page>

To download complete extension please click here

(Visited 17 times, 1 visits today)