Best practice for adding JavaScript code to WordPress plugins

This article is intended as a guide on how to add JavaScript code to your plugins.

Here is a practical example from WP Wall plugin.

Start by hooking your function to wp_print_action:

add_action('wp_print_scripts', 'WPWall_ScriptsAction');

Next, add the scripts you want to use:

function WPWall_ScriptsAction()
 $wp_wall_plugin_url = trailingslashit( get_bloginfo('wpurl') ).PLUGINDIR.'/'. dirname( plugin_basename(__FILE__) );

 if (!is_admin())
	  wp_enqueue_script('wp_wall_script', $wp_wall_plugin_url.'/wp-wall.js', array('jquery', 'jquery-form'));
	  wp_localize_script( 'wp_wall_script', 'WPWallSettings', array(
	  	'refreshtime' => 5,
                'mode' => "auto"

The code above checks if the opened page is not an admin page as we do not want to load scripts there. Next we use wp_enqueue_script function which can add predefined scripts (like jquery, thickbox, sack etc.) or your own scripts to the printing 'queue'. This will ensure that all scripts will be loaded only once even if referenced multiple times by different plugins

Passing parameters to JavaScript using wp_localize_script

WordPress provides an elegant way to pass parameters to your JavaScript code using wp_localize_script function.

WordPress will automatically add the parameters before the call to your script, even using CDATA for validation.

In the previous example refreshtime and mode parameters were passed and can be easily accessed in JavaScript:

refresh = parseInt( WPWallSettings.refreshtime );

mode = WPWallSettings.mode;

Load scripts only when needed

In the example we had a simple check to make sure the scripts are not loaded on admin pages.

In admin pages you can specify exactly on what pages you want the scripts to load.

add_action( "admin_print_scripts-options.php", 'my_admin_scripts' );

function my_admin_scripts() {
  // wp_enqueue_script...

The admin_print_scripts action can be used, and you just need to append the name of the page. In the previous example scripts would load only on /wp-admin/options.php page.


Using this way to add JavaScript to your code is recommended for all plugins. Not only it is elegant and efficient but it is usually faster than manually printing your scripts and parameters.

More like this:

Posted in: WordPress
TAGS:, , , , , , , , , , , , , , ,
Both comments and trackbacks are currently closed.


  1. Apr 22nd, 2010 7:28 PM

    If conditionally passing objects into javascript with wp_localize_script, and to error proof your javascript, always check that the object is available to your javascript and defined before accessing it:

    if ( ! ( 'undefined' === typeof WPWallSetting ) )
    refresh = parseInt( WPWallSettings.refreshtime );
    mode = WPWallSettings.mode;

  2. Duke
    Apr 7th, 2010 11:55 AM

    Hello and thank you for this article :)
    I was wondering if you could maybe provide further instructions for wp_localize_script, as there is no documentation of it that explains in details how to use, and it's not something you see often in plugins.
    Thank you!

  3. Jan 14th, 2010 11:36 AM

    Vladimir, today I posted about doing conditional script and style loading on regular blog pages, rather than the admin ones, as there is no obvious mechanism like the admin_print_scripts- method to do so on post pages right now.

    Please see How To Include CSS and JavaScript Conditionally And Only When Needed By The Posts:

  4. Dec 6th, 2009 2:55 PM

    Hi Valdimir,

    Thanks for pointing out the wp_localize_script funciton. I'd previously been looking for how to localize scripts.

    Should strings to be translated still be enclosed in the __() function?

    What I mean is, should your example read:
    'mode' => __("auto")

    In the script-loader.php core file, I noticed this is how it is done.

    Thanks again.


  5. Sep 11th, 2009 8:57 PM

    Great tips, and great book
    this type of tips helps a lot of new wordpress developers

  6. May 20th, 2009 3:33 AM

    Thanks for sharing your your expertise on developing WP plugins. They've been a great help, especially the tips on loading javascript properly and improving security with nonce.

  7. Apr 20th, 2009 10:53 PM

    Excellent advise. It really help me to add JS script. Before I read your post I spent more the 3 hours trying to add JS script into my plugin.

  8. Peter
    Mar 26th, 2009 10:57 AM

    Quote :

    if (!is_admin())

    The code above checks if the opened page is not an admin page as we do not want to load scripts there. "

    You have no idea how much that sentence helped me. It avoided me loading unnecessary Javascript files into the admin pages.

    Thank you so much and thanks for sharing this.

  9. Mike
    Feb 24th, 2009 7:14 AM

    Awesome - this is just what I needed.

  10. Dec 16th, 2008 7:50 PM

    Thank you, I was looking for something like this :)

    Extremely useful!!!

  11. Dec 3rd, 2008 6:03 AM

    thank you, its very useful

  12. Jake
    Dec 1st, 2008 2:50 PM

    Thanks for the tutorial!

  13. Dec 1st, 2008 12:03 PM

    i tried it