Have a lot of Wordpress plugins? You may need server web hosting if you’ve got limited server space. Whether you use Outlook or Exchange 2007 for your email, you can get it hosted at an outside company so that you save room on your own server. Outsourcing Microsoft Exchange will definitely save you time, money and server
space.
In the process of modifying my Pre-Publish Reminders plugin for WordPress, I came upon an instance where I wanted to use a javascript library that is packaged with WordPress by default, but I couldn’t figure out how. However, after a little bit of frustration, followed by a little bit of detective work, I found an elegant solution included in the WordPress core.
First, let me share with you what doesn’t work. I originally had planned on placing things in the admin <head> section like so:
function nfoppr_add_js_libs() {
?>
}
Now, this approach didn’t work. The reason is that ABSPATH has to do with the file system and not the URL of the blog. So you end up with something like:
... src="./home/usr/nickohrn/wp-includes/js/prototype.js" ..
and this will not include your script. So, I had to find an alternative that would work. Searching through the function reference on WordPress I came across a note about the WP_Scripts object. This looked promising so I started to dig into the core WordPress files. I ended up finding the function wp_enqueue_scripts() within /wp-includes/script_loader.php which allows you to add a script to the <head> section of the page very easily. The function call looks like this:
wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false );
If WordPress recognizes the handle you provide, the library will be added for you. If it doesn’t support calling the library by handle (or the library is something custom you wrote) you’ll have to supply the path to the file as a second argument. The third argument lists any dependencies that the library you are loading may have (in the form of an array or false if there are no dependences). Finally, the fourth argument is the script version which, according to the documentation is primarily for cache busting.
To use the function, you’d do something like this:
function nfoppr_add_js_libs() {
//Add the Prototype framework to the header
wp_enqueue_script('prototype');
wp_enqueue_script('my_custom_prototype_extension', 'path/to/my/lib', array('prototype'));
}
//When you're in the admin menu, add the javascript libraries
add_action('admin_menu', 'nfoppr_add_js_libs');
As I couldn’t find a comprehensive listing of what library names you can call, I’ve decided to include one here. In the following table you can find the full name and handle for each library that is include by default. If applicable, I’ve included a link to the script homepage for each of the libraries that noted one within their source. There is a separate list of libraries that can be included if the logged in user is an administrator, but I’ve left those off the list for now.
So there you have it. I think this is a really elegant solution. Plus, it prevents you from loading the same javascript library multiple times if multiple plugins are using it. If you have any questions, feel free to ask me, and I’ll try to sort things out, or you can refer to the WordPress documentation (although it didn’t have much reference to this at all.)