Search code examples
javascriptwordpressfooteradblockinline-scripting

Add inline Javascripts on Wordpress


I want to add inline Javascript on footer of my wordpress site. I know we can add inline Javascript using wp_add_inline_script. From my understanding from Codex tutorial, we have to register and enqueue scripts and then using wp_add_inline_script.

Let say I:

  1. Register ScriptA.js using wp_register_script.
  2. Enqueue ScriptA.js using wp_enqueue_script.
  3. Add inline ScriptB.js using wp_add_inline_script with handle ScriptA.js.

My Question:

  1. As far as I can remember, Adblock Plus (Web browser ext) or similar can block external .js. If my ScriptA.js being blocked by Adblock Plus, will my ScriptB.js loaded?
  2. IF it is blocked, is there any other proper way I can add inline Javascripts? The only I can think is simply print (echo) it using wp_footer hook.

Reason using inline Javascripts: To prevent Adblock user from blocking my Ads script. Because Adblock Plus cannot block inline script as said by their Dev.


Solution

  • DISCLAIMER: This is not my own answer. I simply copy paste this answer from Wordpress support forum. Credit goes to @bcworkz (Wordpress moderator/developer).

    The inline script is still output even if the enqueued file request is blocked. AFAIK anyway, not verified, but I’m pretty sure this is the case. The inline output cannot know the enqueued request was blocked, the block is external to the server. Of course, the added inline cannot be dependent on the blocked enqueued script if it is to work correctly. Your ad content would need to be part of the inline script, any external requests can still be blocked.

    While AdBlock cannot block inline script, it can remove specific DOM elements. The key is to avoid having any consistent identifiers in your ad elements.