2

I am trying to edit my index.php file in my website using the Joomla! extension manager. I have to add the code right before my closing tag however there is no HTML! So I've tried adding some, after closing the PHP section but as soon as I save it - there's an internal server error.

"""

<?php


* Define the application's minimum supported PHP version as a constant so it can be 
referenced within the application.
*/
define('JOOMLA_MINIMUM_PHP', '5.3.10');

if (version_compare(PHP_VERSION, JOOMLA_MINIMUM_PHP, '<'))
{
   die('Your host needs to use PHP ' . JOOMLA_MINIMUM_PHP . ' or higher to run this 
   version of Joomla!');
}


$startTime = microtime(1);
$startMem  = memory_get_usage();


define('_JEXEC', 1);

if (file_exists(__DIR__ . '/defines.php'))
{
 include_once __DIR__ . '/defines.php';
}

if (!defined('_JDEFINES'))
{
    define('JPATH_BASE', __DIR__);
    require_once JPATH_BASE . '/includes/defines.php';
}

require_once JPATH_BASE . '/includes/framework.php';


JDEBUG ? JProfiler::getInstance('Application')->setStart($startTime, $startMem)- 
>mark('afterLoad') : null;


$app = JFactory::getApplication('site');


$app->execute();

"""

So I have tried this:

"""

<?php


* Define the application's minimum supported PHP version as a constant so it can be 
referenced within the application.
*/
define('JOOMLA_MINIMUM_PHP', '5.3.10');

if (version_compare(PHP_VERSION, JOOMLA_MINIMUM_PHP, '<'))
{
   die('Your host needs to use PHP ' . JOOMLA_MINIMUM_PHP . ' or higher to run this 
   version of Joomla!');
}


$startTime = microtime(1);
$startMem  = memory_get_usage();


define('_JEXEC', 1);

if (file_exists(__DIR__ . '/defines.php'))
{
 include_once __DIR__ . '/defines.php';
}

if (!defined('_JDEFINES'))
{
    define('JPATH_BASE', __DIR__);
    require_once JPATH_BASE . '/includes/defines.php';
}

require_once JPATH_BASE . '/includes/framework.php';


JDEBUG ? JProfiler::getInstance('Application')->setStart($startTime, $startMem)- 
>mark('afterLoad') : null;


$app = JFactory::getApplication('site');


$app->execute();

<!DOCTYPE html>
    <html>
        <body>
            <script></script>
        </body>
    </html>

"""

Yes I did put the actual script code in, for this question I just put the script tag. I am trying to add hubspot webchat by this script:

"""

 <script type="text/javascript" id="hs-script-loader" async defer 
 src="//js-na1.hs-scripts.com/********.js"></script>

"""

******** are numbers that I edited out

5
  • 4
    Joomla is a CMS, not a static page builder. You cannot just inject HTML in any file and expect it to work. Please also note you should never edit Joomla's core files, but instead make use of extensions, be it pre-made ones, or your own. If you could provide more information as to what you're trying to acheive, someone may be able to help you.
    – Lodder
    Jan 11 at 16:14
  • I edited it to show what I have been trying to achieve. Do you recommend any extensions to do that? Jan 11 at 16:24
  • It's probably simplest to add that to the index.php in your template's directory Jan 11 at 16:32
  • 2
    You could use this: extensions.joomla.org/extension/headtag
    – Lodder
    Jan 11 at 16:51
  • This is similar to the question at: joomla.stackexchange.com/q/29230/120 Jan 19 at 23:19

1 Answer 1

2

There are a few ways in Joomla to solve this problem. Some ideas in order of best practice to not recommended are:

1. Template "Advanced" or "Custom" Feature

Check if your template has an option to insert code. For example, The Helix Ultimate template from JoomShaper allows you to insert code in Before </head>, After <body> and Before </body>.

2. Third Party Extension

Use a third party extension such as the free HeadTag plugin by Michael Richey.

3. Custom HTML Module with Sourcerer or similar

JavaScript does not necessarily have to be loaded in the HEAD section of a web page. You could paste the code into a Custom HTML module using the free or paid version of Sourcerer or similar so the code is not stripped when you save the module. Choose "debug" or similar for the module position.

An advantage of using a module is that you can specify which pages the code is loaded on via the Menu Assignment tab (only load the code on pages where the code is needed so performance is better on pages that don't need the code).

4. Add the code to the template index.php file

You can add the code to your template index.php file which can be found at /templates/[YOURTEMPLATE]/index.php

This is usually OK to do for testing but is not a good long term solution as template updates can overwrite and undo your changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.