Friday, June 22, 2012

How to load JavaScript in WordPress plugins

If you want to load some JavaScript files from your WordPress plugin you have at least two options.

Head hooks

When building up a page, just before the closing tag of the head element, a theme should call actions hooked on wp-head. Those actions only need to output what they want to include into the head element, like script files.
What if a theme does not do that? Well, I’d include it into the so called “known issues”. For example, my last WordPress plugin, won’t work in this case. I think it’s better not to find a workaround, so that theme authors understand that they must follow WordPress standards.
On the other hand, there is also an admin-head hook, which is the wp-head hook counterpart when building up admin pages. For example, you could use the following function.

 
function load_into_head() {  
    $ADMIN_VIEW = "alert( 'Hello Admin!' );"; 
    $USERS_VIEW = "alert( 'Hello World!' );"; 
    ?> 
 type="text/javascript">            if( is_admin() ) { echo $ADMIN_VIEW; } 
        else             { echo $USERS_VIEW; }  
    ?> 
    } 
 
add_action( is_admin() ? 'admin_head' : 'wp_head''load_into_head' ); 
 ?>

Script API

WordPress provides also a good Script API that will let you do anything you want with script files, following WordPress standards. These are the main functions:
  1. wp_deregister_script
  2. wp_register_script
  3. wp_enqueue_script
  4. wp_print_scripts (action hook)
  5. print_scripts_array (filter hook)
The general idea is that you write a function for loading your scripts using wp_deregister_script, wp_register_script, and wp_enqueue_script, and hook it on wp_print_scripts. If you also need to fine tune the order in which files are loaded or if they have to be loaded at all, then you can write another function and hook it on print_scripts_array.
deregister, register, and enqueue
wp_register_script let’s you create an alias and define dependencies for each script file. If a script file with the same alias was already registered, then your registration is ignored. This is a policy for conflict resolution (the first registration wins) that may help you, if you know it.
wp_deregister_script let’s you remove an alias. If the alias you give doesn’t exist, nothing happens. Tipically you use this function for forcing a new registration of an already registered alias: first you deregister and then register again.
wp_enqueue_script prepares the loading of a script. If the script was registered with some dependencies, this function automatically prepares their loading too, recursively, making sure each script will be loaded only once and before any script depending on it.
For example, jQuery 1.2.3 is registered by WordPress 2.5, but let’s say that you want to always download the latest version. You could use the following function.

 
function load_with_api() { 
    wp_deregister_script( 'jquery' ); 
    wp_register_script( 'jquery''http://code.jquery.com/jquery-latest.pack.js'false'' ); 
 
    //keep jQuery and Prototype compatible 
    $url = get_bloginfo('wpurl').'/wp-content/plugins/my-plugin'; 
    wp_register_script( 'jquery_no_conflict'$url . '/jquery_no_conflict.js'array'jquery' ), '' );     
    wp_enqueue_script( 'jquery_no_conflict' ); 
} 
 
add_action( 'wp_print_scripts''myOwnTheme_init' ); 
 ?>
 
Keeping jQuery and Prototype compatible is a needed functionality, because WordPress uses both and they both use $ as a global symbol. In fact, the jQuery file packed with WordPress includes the compatibility setting.
Fine tuning
If you want your scripts to be loaded in a particular order, with one script before or after another, maybe with respect to one that was registered by WordPress itself, or by other plugins, then you can write a function for the print_scripts_array filter.
For example, to load the jquery_no_conflict file just after the jquery file, you can use the following function.

 
function jquery_no_conflict_follows_jquery( $js_array ) { 
    if ( false === $jquery = array_search( 'jquery'$js_array ) ) 
        return $js_array; 
 
    if ( false === $jquery_no_conflict = array_search( 'jquery_no_conflict'$js_array ) ) 
        return $js_array; 
 
    if ( $jquery_no_conflict == $jquery + 1 ) 
        return $js_array; 
 
    array_splice( $js_array$jquery + 10'jquery_no_conflict' ); 
 
    unset($js_array[$jquery_no_conflict + ($jquery_no_conflict < $jquery ? 0 : 1)]); 
 
    return $js_array; 
} 
 
add_filter( 'print_scripts_array''jquery_no_conflict_follows_jquery' ); 
 ?>

0 comments:

Post a Comment

I am a student of MS(CS), and also a computer software engineer and web developer.