4

As per title, is there already an event that can work like a sort of OnAfterCompileHead?

Maybe onAfterDispatch, onAfterRender or onBeforeRender?

I need to change some meta tags in the <head> like canonical, title, and so on.

  • are you using any plugin for canonical urls?? – Joomler Jul 15 '16 at 6:48
  • @Joomler Nope, I'm trying to do some custom edits myself to canonical via custom plugin – Gruber Jul 15 '16 at 12:54
5

I have to edit my answer, as I think I didn't get right what you are actually want initially.

With all the 3 events you can do changes on the head. If you have other plugins that do the same, then you can re-order them.

  • onAfterDispatch
  • onBeforeRender
  • onBeforeCompileHead

To edit the <head> data:

The head is stored in the document object and you can access it and manipulate it easily:

$doc = JFactory::getDocument();

$doc->setTitle('My Title');
$doc->setMetaData( 'tagname', 'tag content' );
$doc->addCustomTag( '<!-- this is a comment -->'); 

Examples:

$doc->setMetaData( 'description', 'My Description' );
$doc->addCustomTag( '<meta property="og:title" content="Custom og Title tag">');

You can also get the full head data with :

$head = $doc->getHeadData();
// echo '<pre>' . print_r($head, true) . '</pre>';

The $head is the array of data destined for the HTML <head> section in array form.
This only returns the data added by the set Methods of the JDoc. It will not return data about files that have been linked directly from html in the template head.

You can then set data to the $head like:

$head['metaTags']['standard']['keywords'] = 'keyword1, keyword2,...';

$doc->setHeadData($head);
  • Hey thank you for your answer, the setHeadData will do the trick! My question was mainly caused by my debugging tool which is Kint. Using Kint in the script I have (which is included at the bottom of the <head> tag) I think is causing the break of the head element and so returning empty values with all my tempted tests to retrieve data from the head. Looking with FFDev Tools I can see all meta tags shifted in the <body> and the head is totally emptied. I need to find a proper way to dump/log the data I need to see without breaking the whole output. – Gruber Jul 15 '16 at 12:52

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.