Some important things to note for Wordpress plugin Authors

I have learned a gadoodle of information over the past 3 weeks of coding the update to CommentLuv and it would have gone a lot smoother had I known these 2 things.

Including jQuery with wp_enqueue_script

This seems to be simple at first but there is an important caveat to note.

You can include the jQuery library manually by echoing out a <script src=”… in your plugin code which works but could cause a problem if a user has changed their wp-content location or there are other plugins including jquery which would mean it gets loaded twice or more which isn’t ideal.

The solution is to use wp_enqueue_script but, it needs to be put in the right place!

If you use the wp_head action to call a function that has wp_enqueue_script in it then the library wont be included, using wp_head is too late in the render process for WP to put in the include.

Instead, I put wp_enqueue_script(’jquery’); under the actions and hooks commands so it is run as early as possible in the execution of the plugin.

The plugins directory constant wp_plugin_dir

This has always tripped me up for some reason but it all became clear recently.

If you want to reference an image or js file in your plugins directory it is a lot better to use a constant than to put in an absolute url. I thought I had solved everything by using the constant WP_PLUGIN_DIR which works with WP2.6 and up but doesn’t work with WP2.5.1 :-(
What if you want to make it compatible with wp2.5? simple!, use this code in your plugin and you can use all the constants for WP2.6

if ( ! defined( 'WP_CONTENT_URL' ) )
define( 'WP_CONTENT_URL', get_option( 'siteurl' ) . '/wp-content' );
if ( ! defined( 'WP_CONTENT_DIR' ) )
define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
if ( ! defined( 'WP_PLUGIN_URL' ) )
define( 'WP_PLUGIN_URL', WP_CONTENT_URL. '/plugins' );
if ( ! defined( 'WP_PLUGIN_DIR' ) )
define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );