[wp-hackers] Re: Plugin interference

gbellucci gabellucci at gmail.com
Thu Apr 10 16:43:35 GMT 2008


Here is a simple example that includes:
   1. registering your scripts and dependencies
   2. gets the requested URI and looks for the name of your registered
plugin file
   3. adds one of several actions that will add information before the
</head> tag

/* Register your scripts with WordPress via the wp script loader*/
register_script(
    'your-script-handle',
    'path-to-script/script_name.js',
     array('jquery', 'depends-on-script-handle', ...), 'your-script-
version-num');

/* get the currently requested URL */
$uri = $_SERVER['REQUEST_URI'];

/* Looking for my plugin's php entry point */
if( stristr($uri, '<your_plugin>.php') != false ) {

   // add action here for injecting scripts or css
   add_action('wp_print_scripts', array('classname',
'function_name'));
   // or
   add_action('wp_admin_head', array('classname', 'function_name'));
   // or
   wp_enqueue_script(...)
   // or
   admin_print_scripts(...);
}
else {
     ; /* not my page - so nothing to do */
}

I'm sure everyone does this a little bit differently, however, the
most important aspect of this is to only inject java script on pages
that you create for your plugin's backend options or on backend pages
that your scripts interact with - not on backend pages that are used
as administration/option pages created for use by other plugins.

Other gotch's include NOT injecting jQuery.js before Prototype.js

Maybe others have suggestions




More information about the wp-hackers mailing list