0

While creating Open Graph extension in Magento I came across an issue- although my metatag is correct, facebook cannot catch my og tag because there is too much code before my meta/og tag.

Has anyone else encountered this and do you have any suggestions?

<reference name="head">
    <block type="catalog/product_view" name="product.opengraph">
        <action method="setTemplate">
            <template>socialtag/opengraph_product.phtml</template>
        </action>
    </block>      
</reference>
1

Here's an alternative solution. It's not nice, and should be considered a hack. It also goes against the purposes of Magento's layout system. Nonetheless, if your requirement is not to override the base head template, then try this:

Inject Content via Event Observer

In your module config XML, add the following event:

...
<global>
    ...
    <events>
        <http_response_send_before>
            <observers>
                <head_modify>
                    <type>singleton</type>
                    <class>Namespace_Module_Model_Observer</class>
                    <method>injectOgTags</method>
                </head_modify>
            </observers>
        </http_response_send_before>
    </events>
    ...
</global>
...

And then you would create an observer class to match:

<?php

class Namespace_Module_Model_Observer
    extends Varien_Event_Observer
{

    public function injectOgTags(Varien_Object $object)
    {
        $response   = $object->getResponse();
        $html       = $response->getBody();

        // Some logic for building up the appropriate meta tags
        // based on URL or product or layout handles
        $html = str_replace('<head>', '<head><meta property="og:title" content="Page title" />', $html);

        $response->setBody($html);
    }

}

So far as I can see, this is only way to solve your problem. Neither the stock head block nor its base template make provisions for adding meta tags above its call for child HTML.

1

I'm not sure what exactly is rendering in the head that pushes your meta tags so far down, nor have I seen this being an issue (except the "rule of thumb" which says to place meta tags as high as possible in the HEAD).

In any case, you might try this:

<reference name="head">
    <block type="catalog/product_view" name="product.opengraph" before="-">
        <action method="setTemplate">
            <template>socialtag/opengraph_product.phtml</template>
        </action>
    </block>      
</reference>

Notice the before="-" attribute. This will get picked up during block generation (see Mage_Core_Model_Layout::_generateBlock) and inserted at the top of the parent block's stack (see Mage_Core_Block_Abstract::insert).

Unfortunately, this alone will not solve your problem, as the stock Magento head template does not render its children in a sorted order. So you will have to override this template yourself and force the sort flag.

To do this, change the head template via layout XML:

<reference name="head">
    <action method="setTemplate">
        <template>your/module/head.phtml</template>
    </action>
</reference>

And in that template render your child HTML like so:

<?php echo $this->getChildHtml('', true, true) ?>

Where the third argument is the sort flag which tells the block to render its children according to your managed order. This will get your block at the top.

On a side note, because you will have to go through all of this trouble anyway to extend the head template, you might also want to bump up the child rendering up near the top of the template (above your CSS/JS).


BONUS:

Why are you leveraging the catalog/product_view block? See this extension for a good boilerplate to compare your code with: https://github.com/thaddeusmt/magento-fb-opengraph/ -- their implementation covers OG tags beyond just products.

  • Thanks for the response but that doesn't address my issue. Since I want to make an extension in Mangento, I don't want to use any customized code in the template file. – Jin Lin Nov 25 '14 at 8:16
  • I'm confused. An extension in Magento, by definition, gives you the opportunity to customize templates. In fact that's what you're already doing in your example above. My solution does the same thing, but in 2 places -- adds your template to the head, and swaps out the head template for another one. – Rick Buczynski Nov 25 '14 at 14:07
  • Yes, but I'm trying to create an extension that anyone can use. Thus I don't want to modify the original template from magento- I want keep head.phtml unchanged and try to solve in the layout or any other way. – Jin Lin Nov 26 '14 at 1:47
  • See my 2nd answer. – Rick Buczynski Nov 26 '14 at 4:07

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.