Loading JavaScript In Footer In WordPress 2.8

As you may all know by now, WordPress 2.8 will have some optimization done on JavaScript as well as CSS. That includes concatenating the scripts/styles, minifying it instead of packing as well as gzipping. I have been paying much attention to the development of WordPress 2.8 especially when it comes to JavaScripts. I have filed 2 suggestion tickets in WordPress Trac, Ticket #8884 and Ticket #8859 which should be able to help plugin authors load their JavaScript easily in the footer of WordPress.

This post shall be a tutorial post and I will demonstrate how to load JavaScript in the theme’s footer in WordPress 2.8 Nightly using my WP-Polls as an example.

### Function: Enqueue Polls JavaScripts/CSS
add_action('wp_enqueue_scripts', 'poll_scripts');
function poll_scripts() {
	wp_enqueue_style('wp-polls', plugins_url('wp-polls/polls-css.css'), false, '2.50', 'all');
	wp_enqueue_script('wp-polls', plugins_url('wp-polls/polls-js.js'), array('jquery'), '2.50', true);
	wp_localize_script('wp-polls', 'pollsL10n', array(
		'ajax_url' => plugins_url('wp-polls/wp-polls.php'),
		'text_wait' => __('Your last request is still being processed. Please wait a while ...', 'wp-polls'),

The poll_scripts() function will hook onto the wp_enqueue_scripts function. This will allow you to enqueue JavaScripts as well as CSS Styles for your plugin.

As the name goes, wp_enqueue_style() will queue and then print out your plugin’s CSS link.

wp_enqueue_script() will queue and then print out the JavaScript link. The difference between WP2.7’s wp_enqueue_script() and WP2.8’s wp_enqueue_script() is the addition of the last argument which is whether to load the JavaScript in the footer. If set to true, it will load it in the footer, PROVIDED that your theme has the following code:

in the theme footer (refer to the default theme footer.php if unsure). Many themes especially self-created ones has failed to include wp_head() in the header.php as well as wp_footer() in the footer.php. The default value of the last argument is false, which means it will load it in the header by default.

wp_localize_script() will allow your to translate strings within your plugin JavaScript. To access the translated variable, you can use “pollsL10n.text_wait” (without the quotes).

This new hook:

do_action('admin_enqueue_scripts', $hook_suffix);

will allow you to enqueue your plugin JavaScript or CSS within the WP-Admin area easily rather than calling many times add_action() repeatedly depending on how many pages your plugin has in the WP-Admin area.

The sample code from WP-Polls to print JavaScripts as well as CSS in the WP-Admin area are as follows:

### Function: Enqueue Polls Stylesheets/JavaScripts In WP-Admin
add_action('admin_enqueue_scripts', 'poll_scripts_admin');
function poll_scripts_admin($hook_suffix) {
	$poll_admin_pages = array('wp-polls/polls-manager.php', 'wp-polls/polls-add.php', 'wp-polls/polls-options.php', 'wp-polls/polls-templates.php', 'wp-polls/polls-uninstall.php');
	if(in_array($hook_suffix, $poll_admin_pages)) {
		wp_enqueue_style('wp-polls-admin', plugins_url('wp-polls/polls-admin-css.css'), false, '2.50', 'all');
		wp_enqueue_script('wp-polls-admin', plugins_url('wp-polls/polls-admin-js.js'), array('jquery'), '2.50', true);
		wp_localize_script('wp-polls-admin', 'pollsAdminL10n', array(
			'admin_ajax_url' => plugins_url('wp-polls/polls-admin-ajax.php'),
			'text_delete_all_logs' => __('Delete All Logs', 'wp-polls'),

Firstly, I assigned all the pages of WP-Polls that has an admin page to an array. Next I check whether the current page is within the array. If it is in the array, it means that the user is accessing WP-Polls admin area and thus, I will proceed on to enqueue the JavaScript or CSS needed in the WP-Polls admin area.

Note 1: When you use wp_enqueue_script(), it will automatically register the script and then print it out, whereas if you use wp_register_script(), you will have to manually print it out by calling wp_print_scripts().

60 thoughts on “Loading JavaScript In Footer In WordPress 2.8

  1. Pingback: Wordpress 2.8 veröffentlicht! | Project-Dragon's Gaming Blog

  2. Pingback: Colecção de truques e dicas para a versão 2.8 | Wordpress PT

  3. Pingback: WordPress 2.8 Tips and Tricks Collection | pc-aras

  4. Pingback: Wordpress 2.8 Released With a Cleaner Widget & Theme Interface | Scott Markman

  5. Pingback: WordPress 2.8: Features and tips — gunnerpress.com

  6. Pingback: Aggiornamento a 2.8 molto dolorosa! | Simone D'Amico

  7. Pingback: Mutakhirkan WordPress ke Versi 2.8 | Aman

  8. Pingback: ericchua.com - Wordpress 2.8 Released

  9. Pingback: Wordpress 2.8 is out | benway.net

  10. Pingback: 10 Incredibly Useful WordPress 2.8 Tutorials

  11. Pingback: WordPress 2.8??for???? | ???????...

  12. Pingback: TinyCode: 1.2.0 – wp_enqueue_style > CSS, JavaScript, Plugin, Update, Warteschlange, WordPress > splash ;)

  13. Pingback: WordPress 2.8??for???? | CKSKY

  14. Pingback: 10 Cool WordPress 2.8 Hacks and Tutorials to tinker with - Daily SEO blog

  15. Pingback: WordPress Versi 2.8 Baker Telah Dirilis | RISOFTE

  16. Pingback: New Features of Wordpress 2.8 | pekakbongol.com

  17. Pingback: Extensive Wordpress 2.8 Getting Started Guide | tripwire magazine

  18. Pingback: Changing the plugin file to utilize wordpress 2.8 optimization | :: HawkEYE Expression ::

  19. Pingback: 10 Cool WordPress 2.8 Hacks and Tutorials to tinker with | rapid-DEV.net

  20. Pingback: 10 tutorial per WordPress 2.8 — Studio404 Web Agency

  21. Pingback: WordPress 2.8 is uit (+Resources) : WordPress Dimensie

  22. Pingback: WordPress 2.8 Tips and Tricks Collection | Jokefunfun

  23. Pingback: 10 ?nan?lmaz Yararl? Wordpress 2.8 Dersi « - SamsuNkL.Com -

  24. Pingback: m1dSt1k blog » Wp 2.8

  25. Pingback: wordpress 2.8 | RockFu.Net - Play with Internet!

  26. Pingback: ??? » Blog Archive » WordPress 2.8?? for ????

  27. Pingback: Pridávanie Javascritp skriptov vo Wordpresse | Hlava bloguje

  28. Pingback: WordPress 2.8.1 Released

  29. Pingback: New And Comprehensive Wordpress 2.8 Tutorial and Hack Toolbox | Graphic and Web Design Blog - Inspiration, Resources and Tools

Comments are closed.