Topic on Project:Support desk

How do I add <meta> tags to the <head> for all pages?

8
24.25.217.102 (talkcontribs)

I'm trying to add an RTA label to a wiki I'm deploying for an adult community, which requires adding a meta tag to the head. However, I find the answer from the FAQ to be confusing and unspecific. Here it is for your convenience:

The OutputPage class includes an addMeta methods which can be used to add meta tags. The RequestContext can be used to get the relevant OutputPage object.

To add further Meta tags just add further lines as last lines of the function addMetaTags() like:

$out->addMeta ( 'description', 'This is a meta description.' );

"How do I add meta tags?", Manual:FAQ

So, where exactly is the correct place to add the further lines? Anywhere in OutputPage.php, or some other file altogether?d

101.160.40.121 (talkcontribs)
CuriosityDriven (talkcontribs)

But how do you add meta tags *to the wiki itself,* such that it is attached to all pages? Those extensions are for allowing editors to add meta tags on a per-article basis.

101.160.40.121 (talkcontribs)

Then use the first suggestion.

CuriosityDriven (talkcontribs)

Thanks, but I think I need something or someone to add clarity, not more technical complexity.

I followed your link, but it didn't make sense to me. So, I went a little further to Manual:Hooks like it suggested, but that was overwhelming. I'm trying my best here (and think I've gotten pretty far on my own), but for the current simple task of adding meta tags to my wiki, I'm still just as confused and now a little more frustrated than yesterday.

  1. Is an extension really required just to add meta tags? Is that really what the FAQ was getting at?
  2. If so, then is it really true that there isn't already a stable, commonly used extension for adding meta tags to all pages?

I've gotten this far on my own, but I just need a bit more clarity than what I'm getting from the documentation (hence why I'm using the support desk). So, would you please shed some light on your answer?

Bahhhhh, I've been at this problem for an absurd number of hours now. I need help. Well, really, I need to sleep. If you are able to clarify anything in the meantime, then I would be extremely grateful.

Jörgi123 (talkcontribs)

I have not yet tried adding a meta tag on one of my wikis, but let's try:

If you don't want to use an extension, you can also add the according code in LocalSettings.php: In LocalSettings.php, define the hook and add a custom function, which the hook should call:

$wgHooks['OutputPageParserOutput'][] = 'onOutputPageParserOutput';

function onOutputPageParserOutput( OutputPage &$out, ParserOutput $parseroutput ) {
	// $out is an instance of the OutputPage object.
	// Add a meta tag
	$out->addMeta( 'rating', 'RTA-5042-1996-1400-1577-RTA' );

	// Required return value of a hook function.
	return true;
}

Basically, $out should give you access to the functions in outputPage.php. An overview is here: https://phabricator.wikimedia.org/diffusion/MW/browse/REL1_26/includes/OutputPage.php. One of these functions is addMeta(), which should allow you to add meta tags to the page.

CuriosityDriven (talkcontribs)

Thank you, that was easy to implement and decipher, so I was able to get the current task done and get an idea of how it works for future tasks.

What works: All articles now have the meta tag. And since it's in LocalSettings.php, there is no need to worry about it being during upgrade, style switch, extension incompatability, etc. Therefore, it is a very efficient, future-proof solution that I think will work for most situations.

What's odd: Certain types of pages (e.g., search results, create new article), have the meta tag twice. Certain other pages (e.g., Special:WhatLinksHere/ArticleTitle) don't load the meta tag at all. I'm noting this because even though the solution meets the needs of my task, others should be aware of it just in case it doesn't meet the needs of their task.

88.130.69.106 (talkcontribs)

For those pages, where the meta tag is missing, the part of MediaWiki, which contains the hook, does not get executed. Obviously that is function addParserOutputMetadata() in OutputPage.php. It might be worth checking, if this behaviour is intended. For your usecase, I consider it to be not a problem as long as pages with content do use this code - and, if I understand you correctly, this is the case.

For those pages, which afterwards include the tag twice, this obviously means that the hook is in these cases executed twice. If this is true, I would consider this a bug. You might want to file a bugreport to get this fixed!

As long as this bug is not yet fixed, you should be able to work around this bug by first checking the existing meta tags. $out->getMetaTags() should return you an array, which you can check for the existance of the "rating" key. Then, only if this key does not yet exist, run $out->addMeta() to add it.

Reply to "How do I add <meta> tags to the <head> for all pages?"