2

I am writing a custom extension, and i wanted to add specific JS and CSS into admin dashboard (on every page), and also different JS/CSS files on front-end...

I have seen many answers, but all of them talk about index.php modifications.. Is not there a simpler method, like worpdress has, something like this phseudo-code:

function AddMyScript(){
    echo '<link rel="stylesheet"..................
    //or $this->AddScript('myCss', 1.01, url......)
}
add_action('wp_head', 'AddMyScript');

also, please add two words about how to implement & call that default.php

1 Answer 1

5

Here is the documentation: https://docs.joomla.org/J3.x:Adding_JavaScript_and_CSS_to_the_page

If you wish to add different css and js to the front and back of the site then you should use a system plugin and test for which client it is like this:

class plgSystemRandomname extends JPlugin
{

   function onBeforeCompileHead()
   {
       $doc = JFactory::getDocument();
       $app = JFactory::getApplication();

       if ($app->isClient('site')){

         // add all yr scripts and stylesheets from the frontend, eg.
         $doc->addScript('/path/to/script.js');

       } elseif ($app->isClient('administrator')) {

         // add all yr js and css for the backend, eg.
         $doc->addScript('/path/to/different_script.js');

      }
   }

}

Here is the documentation for making a plugin: https://docs.joomla.org/J3.x:Creating_a_Plugin_for_Joomla

Note that isClient came in in J3.7 I think.

2
  • 1
    exactly what i wanted! However, onBeforeCompileHead was not documented on that page, and that's why I asked.
    – T.Todua
    Commented Sep 4, 2017 at 6:45
  • They are all listed here if you or anyone else needs them @T.Todua docs.joomla.org/Special:MyLanguage/Plugin/Events
    – Eoin
    Commented Jan 11, 2019 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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