2

I try to modify the generated <head> of a Joomla website 3.9.13. Unfortunately I faced on the famous <jdoc:include type="head" /> How can I modify it?

I could not find any reference if this is built from database or php files.

UPDATE

in source code there is that

<!-- JDOC HEAD-->
<jdoc:include type="head" />
<!-- JDOC HEAD-->

in view source is that

<!-- JDOC HEAD-->
<base href="https://xxxxx.gr/" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="keywords" content="PanosK2" />
<meta name="rights" content="PanosK5" />
<meta name="author" content="PanosK4" />
<meta name="xreference" content="PanosK6" />
<meta name="description" content="PanosK1" />
<title>Φωτο</title>
<link href="/templates/tmpl/favicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon" />
<link href="https://xxxxx.gr/anazitisi?id=1&amp;format=opensearch" rel="search" title="Αναζήτηση" type="application/opensearchdescription+xml" />
<link href="https://xxxxx.gr/media/com_uniterevolution2/assets/rs-plugin/css/settings.css" rel="stylesheet" type="text/css" />
<link href="https://xxxxx.gr/media/com_uniterevolution2/assets/rs-plugin/css/dynamic-captions.css" rel="stylesheet" type="text/css" />
<link href="https://xxxxx.gr/media/com_uniterevolution2/assets/rs-plugin/css/static-captions.css" rel="stylesheet" type="text/css" />
<script src="/media/system/js/html5fallback.js" type="text/javascript"></script>
<!-- JDOC HEAD-->

For example how can I find which extension (or php file) creates the last line?

  • 1
    what would you like to do or modify in <head> ? – Zollie Dec 6 '19 at 11:46
  • I found in the page source some lines of code that I want to modify. (eg. <script src="/media/system/js/html5fallback.js" type="text/javascript"></script>) I want with reverse engineering to be able to tamper the head that Joomla creates. Even better to find which module creates it. – Panos Dec 6 '19 at 15:39
  • 1
    Please edit your question in a definitive/finalising way so that volunteers don't have to hit a moving target. Show us exactly what you are working with, what you've done, what you're currently seeing, and what you need. The clearer the question, the better the answers (with less volunteer time wasted). – mickmackusa Dec 7 '19 at 0:02
2

You get the `HtmlDocument' object:

$doc = JFactory::getDocument();
// echo "<br><pre>"; print_r($doc); echo "</pre>"; // to check what you have here

You can get only the head Data:

$headData = $doc->getHeadData();
$Title = $doc->getTitle();

You can set things:

$doc->setHeadData(array $data);
$doc->setTitle('my title');

And you can see all of the things you can do in this class here: https://api.joomla.org/cms-3/classes/Joomla.CMS.Document.HtmlDocument.html

and here: https://api.joomla.org/cms-3/classes/Joomla.CMS.Document.Document.html

EDIT:

On the updated part of your question: In Joomla you do not really have to know which extension is adding a script in the head as far as deleting that script does not cause problems. And as I see in your case you are determined to delete that script from the <head>of your site. I also do not know your expert level of PHP and Joomla thus I'd like to provide you two approaches here.

First the simplest and a bit hacky solution which is not recommended by more serious Joomla developers and the reason it is not recommended is mainly because you will write few lines directly in your template's index.php file. If the template will be updated, your few lines of added code is going to be deleted too by the template update. But maybe you at the moment do not care about this and after the occurent template update you can still copy these few lines back in the index.php file again if you would like to.

So, place these few lines of PHP code in your template's index.php file right after the line: <jdoc:include type="head" /> :

if (isset($this->_scripts['/media/system/js/html5fallback.js'])) {
    unset($this->_scripts['/media/system/js/html5fallback.js']);
}

This will delete the unwanted script loading in the <head>.

Now, the second and standard Joomla solution for these kind of tasks is creating a Joomla system plugin on the event: 'onBeforeCompileHead' which event is triggered in the HeadRenderer.php file in Joomla which does what it's name suggests. (file location in Joomla: libraries/src/Document/Renderer/Html/HeadRenderer.php)

Since few weeks ago someone here has given a good example of this kind of plugin, I'll not repeat the code example of that plugin as it is quite self explanatory after a short time of studying these two posts here:

The system plugin example: onAfterRender plugin only triggers on backend

And the plugin code part here in this post's answer which you should use in your plugin and you can delete anything from the head with using this or something very similar plugin code:

How can I completely remove Bootstrap from the frontend of Joomla?

I copy here the meat of this plugin though:

class plgSystemYourPlugin extends JPlugin
{
    public function onBeforeCompileHead()
    {

        $doc = JFactory::getDocument();

        // Remove the unwanted script
        if (isset($doc->_scripts['/media/system/js/html5fallback.js'])) {
            unset($doc->_scripts['/media/system/js/html5fallback.js']);
        }

    }
}

This plugin can be written very fast actually and if you would like to create one, you can study the subject more here: https://docs.joomla.org/J3.x:Creating_a_Plugin_for_Joomla

I hope the above will help you more.

  • $doc and $headdata do not include the data that <jdoc:include type="head" /> returns – Panos Dec 6 '19 at 15:58
  • @Panos - You probably misunderstand these things a little bit. Joomla is a quite huge framework and a Joomla site is built with a lot of different Documents. So it depends on which page, or which view you are calling the current document object from, you will get a different document back with different content and structure. In the meantime jdoc:include statements is at the render or parsing part of the documents and usually used in the templates of the Joomla applications. So where do you use these that will determine what you will get back. – Zollie Dec 6 '19 at 18:09
  • @Panos - now your question is changed quite relevantly, since first you asked: how do you reach and modify the <head> content of a document in Joomla? And now you ask: How do you know which component, module or plugin adds something in the head of a document? That is quite a different question now. :) – Zollie Dec 6 '19 at 18:13
  • Only tomorrow I’ll have more time to give you an extended answer on your updated, modified question. I’ll try to help you on that too. Are you working in the index.php file of your template or where? What template do you use as default on your site at the moment? – Zollie Dec 6 '19 at 18:32
  • Thank you for your effort to help me. I may not explained my problem correctly. I am working in index.php. the template I use is the braink. – Panos Dec 6 '19 at 19:25
0

Joomla has an HTMLHelper class that will let you do a lot of modifications to the head, depending upon what you want to do. You can use its methods directly or use the HTMLDocument class to access them.

Examples:

HTMLDocument

  • addHeadLink lets you add a link element to the generated head.
  • addCustomTag lets you add any HTML string to the head
  • addFavicon lets to add a favicon link

Document (HTMLDocument inherits from this)

  • addScript and addScriptDeclaration add javascripts
  • addStylesheet and addStyleDeclaration add CSS
  • setCharset sets the character set
  • setLang sets the language
  • setTitle sets the document title

Without knowing what you're trying to do, it's hard to come up with a good answer, but I hope this gets you started towards your objective, anyway. Feel free to come back and edit your question with more details if you're looking to do something specific that this doesn't help with.

  • I found in the page source some lines of code that I want to modify. (eg. <script src="/media/system/js/html5fallback.js" type="text/javascript"></script>) I want with reverse engineering to be able to tamper the head that Joomla creates. – Panos Dec 6 '19 at 15:33
  • That's not a part of Joomla (at least not 3.9.x) so it's being put there ostensibly by another addition that thinks it needs it. But typically you can override medis/system/js loads by simply including an empty file of the same name (html5.fallback.js) in your template (/js/jui/html5fallback.js). If whatever is loading that file is written correctly, this will result in an empty file taking its place. – Arlen Dec 6 '19 at 15:57
  • I tried your solution with no luck. I created the file but there was anything written inside. Any other thoughts about how can I find which is "write" in '<jdoc:include type="head"/>' and how? – Panos Dec 6 '19 at 17:26
  • Gonna have to regroup on that last comment. It is part of Joomla. The good news is it's called for by a layout, and they're trivial to override in your templates. – Arlen Dec 6 '19 at 21:38
  • Gaaah. Hate hitting return by accident. The layout files are /joomla/form/field/text.php and /joomla/form/field/password.php so in your template folder you can create the overrides (in /html/layouts/joomla/form/field/) by copying in those two files, then deleting the line that loads that file. Should be fairly safe to do, because it's just there to support inept browsers that don't understand html5. – Arlen Dec 6 '19 at 21:42

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.