Is there any content plugin that let me to put any text/code/script before closing </body>
tag? If there is no such plugin, is there any documentation about how to create that kind of plugin?
2 Answers
Plugin code
<?php
// no direct access
defined('JPATH_BASE') or die;
class plgSystemMyplugin extends JPlugin
{
function onAfterRender()
{
$app = JFactory::getApplication();
if ($app->getName() != 'site') {
return true;
}
$buffer = JFactory::getApplication()->getBody();
$insert = 'insertthis';
$buffer= str_ireplace('</body>',$insert.'</body>',$buffer);
JFactory::getApplication()->setBody($buffer);
return true;
}
}
-
1There are
appendBody()
andprependBody()
methods worth reading up on. Commented Jan 9, 2019 at 16:17 -
1
Why use a Joomla plugin? You can just do the following:
- Add a position in the template (e.g. in your
templates/template-name/index.php
) just before the closing</body>
tag. Let's call this positioncode
. - Create a Custom HTML module (now called a Custom module), and add your script code to it, and then assign it to the
code
position and to the appropriate pages. - Remove the enclosing div tag added to the Custom HTML module as described here.
I don't think in your scenario you should really use a plugin as a module will suffice and will do exactly what you need to do (it is also a cleaner approach, since plugins add to the overhead of all the pages).
-
I use the modal extensively, sometimes in one article I need to put 2-3 modals, between paragraphs– webchunCommented Dec 4, 2016 at 10:17
-
-
It's the bootstrap modals. Anyway, I actually know some workarounds for the modal issue, but in this question, I'm looking for answer that is telling me whether it's possible to add acript/code before closing body tag or not, and if it's possible, how to do that. I will update the question– webchunCommented Dec 4, 2016 at 10:36
-
1The answer that I provided explains how to add script before a closing body tag. You can use a custom HTML module to do that. Maybe I missed something though, I'll wait for you to update your question. Commented Dec 4, 2016 at 10:39
-
I want to achieve the same thing @itoctopus and the reason I can't use your method is because I am creating a module that I want to be able to distribute to others without them having to take that step.– EoinCommented May 21, 2021 at 17:44