1

I need to integrate addthis widget on CMS page. In my custom them I created xml file cms_page_view.xml which resides at app/design/frontend/Vendor/Themename/Magento_Theme/layout/

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no"/>
        <script src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-1234567890" src_type="url" />
    </head>
</page>

I can see the social sharing icons appearing on that CMS page, but as I have included it from xml, it appears on all CMS page of the store

I tried including below xml from Design tab in Layout Update XML in admin

<reference name="head">
    <block type="core/text" name="shop-by-project">
        <action method="setText"><text><![CDATA[<script src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-1234567890" />]]></text></action>
    </block>
</reference>

It gives exception Please correct the XML data and try again. Element 'reference': This element is not expected. Expected is one of ( referenceContainer, container, update, move ). Line: 1

  • How do I include external and internal javascript for specific CMS page making sure it does not load in other CMS and product pages?
2

Instead of cms_page_view.xml handle, you can try cms page specific layout handle as shown below:

+-----------------------+---------------------------  +
|    Page Identifier    |  Layout Handle Name         |
+------------+----------+-----------------------------+
| Home                  | cms_index_index_id_home     |
| about_us              | cms_index_index_id_about_us |
+-----------------------+-----------------------------+

i.e, append your page identifier with cms_index_index_id_ and you get your specific cms page layout update handle.

  • Thanks for responding, but I have around 20 CMS pages which supports social sharing. So does it mean I have to create 20 xml files with different page identifiers? – Slimshadddyyy Mar 23 '18 at 12:40
  • From your question, what I understand is, you need to integrate some js with a specific cms page. In that case, you need to use the specific layout handle as I mentioned in my answer. If you want to add this functionality for 5 cms pages out of 20 cms pages, then you need to create 5 specific layout handle files to achieve this. Or else you need to do this via event observer method, I believe – Rajeev K Tomy Mar 23 '18 at 12:46
1

Your syntax is wrong (you seem to be using Magento 1 syntax), there is no container or block named head.

Try this:

<head>
    <script src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-1234567890" src_type="url" />
</head>

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.