Problem: Wordpress plugin loads javascript on every page

phrench

Not yet banned
Mar 10, 2008
1,387
21
0
/dev/null
Anybody knows how to tell a wordpress plugin to only include the JS when the plugin shortcode is used within a page/post?

I don't want to slow down the whole website only because I use that plugin on 3 pages.
 


The plugin makes already use of the function add_shortcode(). And the Jedi Master Way in the link I posted as well.

But unfortunately the plugin is coded in a very nasty way. Haven't seen such dirty code in a while and I would have to change too much in the code to apply the solution from the link I posted :-/
 
I discovered this today which may help someone.

Wordpress loads jQuery in "no conflict mode" by default. That means that this will not work:

Code:
$(document).ready(function() {
  // Handler for .ready() called.
  alert('test');
  console.log('test');
});

You will get "$ is not a function" errors. To remedy this, you need to wrap it like so:

Code:
jQuery(document).ready(function($) {
  // Handler for .ready() called.
  alert('test');
  console.log('test');
});

Then inside your .ready() function you can use $. just like you normally would.