WordPress Plugin Developer Day

Matt has announced today (28th January 2009, 29th in my timezone) to be WordPress Plugin Developer Day.

WordPress by itself is very simple – what makes it compelling for most of its users is the wide array of plugins (and themes) available for WP. The average WordPress blog has about 5 plugins installed! Today we just passed 4,000 plugins available in our plugin directory. (Which is also embedded into everyone’s WordPress 2.7 or above.)

I declare January 28th our official “Thank a Plugin Developer” day. To celebrate take a look at the plugins you use and love, visit the author’s site, find their contact form, and drop them a note thanking them. (Or Paypal!) Look for the links in the plugin directory to “author homepage” and also to donate directly if they’ve specified a Paypal address.

Thank you to everyone who has ever written a plugin for WordPress, and here’s to the next four thousand.
🙂

Donations I received today:

  1. John Kolbert – http://www.johnkolbert.com/
  2. WP Candy Team – http://wpcandy.com/
1 Star2 Stars3 Stars4 Stars5 Stars (178 votes, average: 3.97 out of 5)

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().

1 Star2 Stars3 Stars4 Stars5 Stars (126 votes, average: 4.08 out of 5)

JavaScript In My Plugins II

I have completed the “update” process to my 5 of my plugins (WP-Polls, WP-PostRatings, WP-Email, WP-ServerInfo and WP-UserOnline) which use JavaScript.

  • WP-Polls and WP-PostRatings uses JavaScript on the frontend as well as the backend
  • WP-Email and WP-UserOnline uses JavaScript on the frontend only
  • WP-ServerInfo uses JavaScript on the backend only

Unfortunately the changes require at least WordPress 2.8 onwards to make use of the JavaScript enhancements in WordPress 2.8. I have updated this site to WordPress 2.8 Bleeding Edge aka WordPress 2.8 Nightly in order to test the JavaScript changes on a live site.

If you view the HTML source of this page, you will notice that only the CSS files get loaded in the header and the JavaScript gets loaded in the footer.

If you are running WordPress 2.8 bleeding edge, perhaps you can help me test out the plugins. Thanks in advanced 🙂

1 Star2 Stars3 Stars4 Stars5 Stars (176 votes, average: 4.17 out of 5)

JavaScript In My Plugins

Only 5 of my plugins (WP-Polls, WP-PostRatings, WP-Email, WP-ServerInfo and WP-UserOnline) use JavaScript. The JavaScripts are packed using Dean Edward’s Packer and the only framework use just for AJAX is TW-Sack or Simple AJAX Code-Kit (SACK). TW-Sack is extremely lightweight and easy to use but unfortunately, it can be used only for AJAX purposes. The reason I used TW-Sack instead of jQuery is simple, at the time I created my plugin, jQuery does not exists yet and when jQuery becomes popular the reason why I didn’t upgrade it was because I was lazy. As the saying goes, “If it ain’t broke, don’t fix it”. Recently there is some buzz within the WordPress Development Team regarding WordPress’s JavaScript Usage. Andrew Ozz has written two articles on it in the WordPress Development Updates blog entitled Optimizing script loading, Optimizing script loading part 2 and Optimizing script loading, implementation. With the upcoming WordPress 2.8 will feature some heavy JavaScript changes, I told myself to make use of this chance to totally revamped the JavaScripts in my plugins. Here are the changes that will be made:

  • Minified JavaScript instead of packing them
  • Replaced TW-Sack with jQuery
  • Move JavaScript to the footer
  • Use wp_localize_script() for JavaScript text translation
  • javascript-js.js will now contain the minified code for normal usage
  • javascript-js.dev.js will now contain the unminified code for development purposes

I have already done it for WP-PostRatings and it is now running live on this site. Next, I will be doing WP-UserOnline, followed by WP-Email and lastly WP-Polls as it is more complex.

1 Star2 Stars3 Stars4 Stars5 Stars (117 votes, average: 3.92 out of 5)
Page 1 of 212