Some important things to note for Wordpress plugin Authors

September
26

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' );

Code, PHP, Wordpress


6 Comments zu “Some important things to note for Wordpress plugin Authors”

  1. witchypoo
    26.09.08 10:48 pm

    Twitter: psychicgeek
    Thank you , Andy. I checked and that is what you included in the latest
    commentluv.php that you sent me. Yer a peach!

    witchypoo´s last blog post..Witty Comebacks

    #1

  2. byteful
    27.09.08 8:33 am

    Hey, thanks for responding to that so quickly!

    I could put that in manually, but I think I’ll be lazy this time and wait for an updated php file. ;-)

    byteful´s last blog post..What an Early Fiery Sunrise Taught Me About Color

    #2

  3. Dennis Edell
    27.09.08 9:16 am

    I have no idea what you just said, but thanks! :-)

    Dennis Edell´s last blog post..New Feature! Link Love For Your Thoughts

    #3

  4. Andy Bailey
    27.09.08 12:01 pm

    witchypoo: my pleasure!

    byteful: see previous response I made on the other post, just redownload 2.1.5 and install it again

    dennis: you’re welcome!

    #4

  5. sunduvan
    27.09.08 3:20 pm

    huhuhu hands up. cannot get it on. help plox.. hehe

    sunduvan´s last blog post..Outfitting Your Dog For Winter

    #5

  6. sunduvan
    27.09.08 3:22 pm

    sorry..I think I got how to do it now. Thanks so much muahhh… hua hua

    sunduvan´s last blog post..Dead End For Google AdSense Is Coming.

    #6