Jump to content

How to disable / override 'deferInlineScripts' function in Media class?


Recommended Posts

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

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!

  • Like 1
Link to comment
Share on other sites

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?

  • Like 1
Link to comment
Share on other sites

  • 1 year later...
  • 1 year later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...