Tuesday, October 5, 2010

Inline scripts in a WordPress plugin

I wanted to include some inline JavaScript code in WordPress pages for performance reasons (a second HTTP request for 200 bytes is silly). I found the wp_enqueue_script API call, but it's only for external scripts. Looking through the entire API, I found the wp_print_scripts hook. It's not elegant, but it works:

function my_script() {
echo '' . "\n";
}

// Register the processing functions with WordPress
add_action('wp_print_scripts', 'my_script');