2

So, I'm trying to modify a plugin. This plugin sets some meta tags in the head section using addCustomTag (adding <meta property="og:...>, etc.) The problem is that they should be added only once. Is it good idea to use $woo = JResponse::getBody(); and to check within with preg_match()? My concern here is that JResponse is deprecated. Event used is onContentAfterDisplay.

1

Add the entry only once by adding a field to your application and check whenever you want to add your custom tag.

$app = JFactory::getApplication();
if(!isset($app->metaloaded))
{
  $document->addCustomTag('<meta property="og:..."/>');
  $app->metaloaded = true;
}

Now if you want to call it at some other place or the function is called twice in the same request, the check will prevent it from executing twice.

To check within already existing custom tags added by other extensions, you can use the below code:

$document = JFactory::getDocument();
$headData = $document->getHeadData();
$existing = false

foreach($headData['custom'] as $tag)
{
   if($tag == $mytag) // or you can use if(strcmp($tag, $mytag) === 0))
   {
       $existing = true;
       break;
   }
}

// now if the tag does not exist, add your tag
if(!$existing)
{
    // add your tag here
}
  • Nice idea, but how to check if this meta already exist (added by some other plugin)? It would be slow, but safe. – pl71 May 15 '15 at 19:25
  • Added another method in my answer to check tags added from other extensions – Nagarjun May 15 '15 at 19:37
  • Thanks, applied both methods finally. But it looks like $woo = JResponse::getBody(); is not working inside onContentAfterDisplay anyway. I need to check HTML of the article also. It should be another question. – pl71 May 17 '15 at 19:51

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.