简体   繁体   中英

How to add ad script in top- header in Prestashop - 1.6?

I am using prestashop 1.6 . I want to add google ads in top of the header and bottom in footer. I tried many ways but all are not succeed. Please how can i add script in my prestashop website? Thanks in advance.

You need to find header.tpl file: https://github.com/PrestaShop/PrestaShop/blob/develop/themes/default-bootstrap/header.tpl

<head>
    {$HOOK_HEADER}
    <link rel="stylesheet" href="http{if Tools::usingSecureMode()}s{/if}://fonts.googleapis.com/css?family=Open+Sans:300,600&amp;subset=latin,latin-ext" type="text/css" media="all" />
    <!--AdWords Code-->
</head>

Remember to disable CCC options for JS (especially moving JavaScript to the end): 在此处输入图片说明

Anything within {literal}{/literal} tags is not interpreted, but displayed as-is

{literal}
<script type="text/javascript">
// ...
</script>
{/literal}

{ldelim} and {rdelim} are used for escaping template delimiters, by default { and } :

<script type="text/javascript">
function foo() {ldelim}
    // ...
{rdelim}
</script>

gives:

<script type="text/javascript">
function foo() {
    // ...
}
</script>

If you still have a problem you may try to override Media Class:

https://gist.github.com/hereswhatidid/8c8edef106ee95138b03

 <p>Some HTML goes here</p>
 <script type="text/javascript" data-keepinline="true">
 // this script will remain here when rendered
 alert( "hello!" );
 </script>
 <script type="text/javascript">
 // this script will be forced to the bottom of the page
 alert( "hello again!" );
 </script>

Media.php:

<?php
Class Media extends MediaCore
{
    public static function deferScript($matches)
    {
        if (!is_array($matches))
            return false;
        $inline = '';

        if (isset($matches[0]))
            $original = trim($matches[0]);

        if (isset($matches[1]))
            $inline = trim($matches[1]);

        /* This is an inline script, add its content to inline scripts stack then remove it from content */
        if (!empty($inline) && preg_match('/<\s*script(?!.*data-keepinline)[^>]*>/ims', $original) !== 0 && Media::$inline_script[] = $inline)
            return '';

        /* This is an external script, if it already belongs to js_files then remove it from content */

        preg_match('/src\s*=\s*["\']?([^"\']*)[^>]/ims', $original, $results);
        if (isset($results[1]) && (in_array($results[1], Context::getContext()->controller->js_files) 
            || in_array($results[1], Media::$inline_script_src)))
            return '';

        /* return original string because no match was found */
        return $original;
    }
}

The correct way should be using a module. Also check if the function htmlpurifier is blocking your scripts tags.

A little late, but it is solved by using {literal} //script here {/literal} . It's supposed to be used only if there are curly brackets in your script, but it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM