5

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?

3

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;
    }
}
2
2

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 position code.
  • 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).

5
  • I use the modal extensively, sometimes in one article I need to put 2-3 modals, between paragraphs
    – webchun
    Dec 4 '16 at 10:17
  • What do you mean by modal?
    – itoctopus
    Dec 4 '16 at 10:19
  • 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
    – webchun
    Dec 4 '16 at 10:36
  • 1
    The 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.
    – itoctopus
    Dec 4 '16 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.
    – Eoin
    May 21 at 17:44

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.