[wp-hackers] Loading JS & CSS from inside a plugin
Dave Ross
dave at davidmichaelross.com
Tue Mar 22 21:01:45 UTC 2011
I'm the author of the Dave's WordPress Live Search plugin, and getting very
frustrated trying to load Javascript and CSS within my plugin.
I've spent way too many hours dealing with all the different configurations
& plugins my users are using, including (but probably not limited to):
* Normal WordPress in Docroot
* Normal WordPress in a subdirectory
* WP Subdomains plugin
* wp-content in a non-standard location
* WPMU
Way back in my plugin's v1.0 days, I included the relevant JS & CSS in the
page's <head>. But that upset a lot of devs who didn't want the same code
included in every page. It's redundant, but at least I could be guaranteed
it would work.
As of the current dev version, I'm hooking into parse_request and returning
the static resource if a query parameter exists.
Here's how I'm loading one of the scripts:
define('DWLS_JS_PARAM', 'dwls_js');
wp_enqueue_script('jquery_dimensions', get_bloginfo('url') . "?" .
DWLS_JS_PARAM . '=dimensions', 'jquery');
It's slower than linking to a static file (WordPress has to start running),
but it works universally -- or so I thought. Today, I got two bug reports
for issues loading resources on WPMU, one of which is using the dev version:
http://wordpress.org/support/topic/plugin-daves-wordpress-live-search-broken-with-domain-mapping-wpmu
My question in the short term is "what am I doing wrong?". Is there a better
way I can link to a resource and be guaranteed it'll work across the board?
For the long term I'd like to request the addition of a new API, similar to
Drupal's "Butler" project, which figures out a context from the site config
and current page request, and can generate correct links to pages & files.
Plugins like WP Subdomains can hook into this API and change the rules it
uses to build URLs. I haven't thought out the details yet, but WP could
really use it.
Any help would be appreciated. I'm spending too much time tracking down
little compatibility issues and it's keeping me from making further
improvements to this plugin or even tackling other WordPress projects.
Thanks,
Dave
More information about the wp-hackers
mailing list