Topic on Project:Support desk

Jump to navigation Jump to search

How can I add the code into the <head>

20 (talkcontribs)

I want to modify the skin so that every page includes a script (adsence) in the head. In which file should I add the code? I use Vector skin. VectorTemplate.php doesn't seem to be the file as it just outputs headelement formed somewhere else. I'd like to know, where?

AhmadF.Cheema (talkcontribs) (talkcontribs)

Thank you, Ahmad! It worked

TheDJ (talkcontribs) (talkcontribs)

How is it possible to insert the script in the head only for non-automatically generated pages? Or specify somewhere explicitly on which pages the script has to be inserted? (talkcontribs)

Anyone please suggest, how can a change the head of specific pages. Thank you!

TheDJ (talkcontribs)

Depends. Is it just dependening on a title ? Then just take the title from the earlier mentioned hook and pageoutput and conditionally add it.

If it depends on the CONTENT being insterted, like with video, graphs atc, then you should only add from the parserhook, which transforms your wikitext to html.

In that case you need to define and use resourceloader modules which declarativly wrap your scripts and then add those modules to the parseroutput using the addModules method. There are many wikimedia installed extensions that do this, that you could look towards to learn from. (talkcontribs)

Thank you TheDJ. Yes, if I can add a scipt in the head depending on the page title, that'd do what I need.

I'm a beginner, so do not completely understand what to do. As a start I tried to just add the script on every page using the hook you suggested.

I wrote in the LocalSettings.php

$wgHooks['OutputPageBeforeHTML'][] = 'addInlineScript(<script >...</script>)';

but it led to "Internal error", so apparently there was error in the syntax. What should I modify in order to add that script?

Thank you.

Ciencia Al Poder (talkcontribs) (talkcontribs)

Thanks, but it adds the script not in the head. How to modify it to add in the head? (talkcontribs)

Please, suggest how it is possible to add a script in the head of a given page? Thank you for any advice!

HSRobinson (talkcontribs)

> Please, suggest how it is possible to add a script in the head of a given page? Thank you for any advice!

If you are using Extension:HeadScript then in LocalSettings.php you must define the JS code (or filename). So you can use PHP to "decide" whether or not to add that code. (talkcontribs)

Thank you. HSRobinson

Could you provide more details on this? Because I tried to use LocalSettings to put the script only in one page and it didn't work:. The code was:

if ($id == 106) {

  $wgHeadScriptCode = <<<'START_END_MARKER'




How can I modify this approach to have the script in the given page? Thank you!

HSRobinson (talkcontribs)

I have not found a way to access the article/page ID in LocalSettings, but you can certainly use $_REQUEST or $_SERVER to get the URL of the page and use that to filter. Or if you want you can use the API to get the ID via the URL.

Del222 (talkcontribs)

Thank you! You mean, that LocalSettings doesn't know, what page id is? So I should work through the url (which might change)?

Your approach worked, just for someone who's trying to do something similar I'll post the piece of code here:

if (($_SERVER['REQUEST_URI'] == '/index.php?title=XXX') || ($_SERVER['REQUEST_URI'] == '/index.php?title=YYY'))


  $wgHeadScriptCode = <<<'START_END_MARKER'

<script> ... </script>



So you can explicitly indicate the pages where you want the script. (talkcontribs) (talkcontribs)

Hello, does anyone know a way to add HTML to the <nowiki><head></nowiki> tag with vanilla JavaScript (ES6/7)? Thanks,

Ciencia Al Poder (talkcontribs)

Usually, adding contents to the head with JavaScript is useless. However, even if you want to do that, that's not really related to MediaWiki, since that's something so generic as "how to add HTML to any arbitrary HTML element" (talkcontribs)

@Ciencia Al Poder I'm curious to know why you consider it useless to add HTML to head with JS? Thanks anyway...

Ciencia Al Poder (talkcontribs)

The kind of code you usually want to add to the head element is for third party software to process, like an authorization key for google analytics or a search engine, OpenGraph tags for social media, etc. But the programs that scrape your website to look at those tags don't execute JavaScript, hence why I think it's usually useless to add those tags with JavaScript.

On the other hand, if the user wants to add a script using javascript, well... it's irrelevant where you put the script tags, because what you want is to execute the script, not to put script tags on the page.

Reply to "How can I add the code into the <head>"