abdulk Posted June 8, 2014 Share Posted June 8, 2014 Hello All, I'm relatively new to PS, but pretty experienced in WordPress. I know enough php and js to be dangerous, but not an expert. I have some JS tracking code that I must add to the footer of every page. Of course, I tried adding it to footer.tpl in my theme, but I read that new in PS 1.6, all JS (including in-line) is deferred and reorganized to the bottom of the footer, which is breaking my script. The problem is that my 3rd party tracking code has additional attributes within the <script> tag that get stripped when it goes through the Media class 'deferInlineScripts' function. I have fixed this by directly editing Media.php and removing most lines within the 'deferInlineScripts' function, but I want to do it properly with an override and can't figure it out. So far, I've created a new file called Media.php and placed it in /override/classes. Here is the content: <?php class PlaydateMedia extends MediaCore { public function deferInlineScripts($output) { return $output; } } ?> I am trying to essentially remove the 'deferInlineScripts' function, but I'm obviously doing it wrong / missing something. I've spent over 10 hours on this so far. I even made my own module to add the code, but no matter what I do that pesky deferInlineScripts function strips out the necessary <script> attributes, breaking the script. Thanks so much! -Abdul Link to comment Share on other sites More sharing options...
abdulk Posted June 8, 2014 Author Share Posted June 8, 2014 Wow, I figured it out! I was on the right track all along! The only edit needed to the previous code is that the class name must be "Media". If you also found this functionally bothersome, here's the final code to disable any processing and deferral of your inline scripts: <?php class Media extends MediaCore { public static function deferInlineScripts($output) { return $output; } } ?> Don't forget to delete /cache/class_index.php after copying the above Media.php to /override/classes. Enjoy! 1 Link to comment Share on other sites More sharing options...
El Patron Posted June 8, 2014 Share Posted June 8, 2014 rather than remove all take a look at this PrestaShop Media class override to allow for forcing some inline JavaScripts to remain inline. https://gist.github.com/hereswhatidid/8c8edef106ee95138b03 Link to comment Share on other sites More sharing options...
abdulk Posted June 12, 2014 Author Share Posted June 12, 2014 Hi El, that worked! I found that when I first searched, but was deterred by all the follow up commits in the original thread. Also, I am really struggling to follow the logic in Media.php. Would you kindly help me understand for my own edification? It looks like deferInlineScripts() finds all the <script> tags with preg_replace_callback and then sends each match to deferScript(). Then, I see the comment in deferScript() /* This is an inline script, add its content to inline scripts stack then remove it from content */ but I can't for the life of me tell how it's doing that. I don't see the match being passed anywhere after that? 1 Link to comment Share on other sites More sharing options...
bteo Posted June 18, 2015 Share Posted June 18, 2015 rather than remove all take a look at this PrestaShop Media class override to allow for forcing some inline JavaScripts to remain inline. https://gist.github.com/hereswhatidid/8c8edef106ee95138b03 Thanks for this! I was about give up when I read your reply. Best solution ever! Works for me thanks a bunch Link to comment Share on other sites More sharing options...
Regaton Posted September 8, 2016 Share Posted September 8, 2016 Hello, Doesn't work in 1.6.1.4... Do you have an idea ? Thanks ! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now