WP Image Preloader

Publicat la 25 martie 2011, în Blog, Freebies, de Adrian7

Great news: version 1.2 of the plugin has been released! Now you can preload more images with a single piece of shortcode.

WP Image Preloader is a plugin for WordPress, made to help you speed up your blog. It pre-loads images using the Javascript technique.

How to preload your images? You can do this in two ways:

  1. Add the relative or absolute url of each imag into the default stack. This way, the image(s) will be preloaded on every page of the blog. Use this if you have hover images for menus.
  2. By adding the shortcode [preload=image_url] for one image or [preload=image_url1, image_url2, image_url3] for more images, into your posts. Note you have to activate the shortcode from the plugin configuration page first. This way the image will be preloaded only when viewing that specific post or page. You can also use relative or absolute urls.

Screenshots:

Documentation:

WP Image Preloader uses a jQuery script to preload images. The basic functionality of the script uses the same document.createElement('img') traditional DOM method. You can find the code in /js/jquery.imagepreloader.js file.

As per server-side, the plugin  uses a global array (stack) $WPIMGP_Queue = array(), to store absolute urls to the images.  The functions used to add urls to the global stack are:  wpimgpreloader_build_default_stack() and wpimgpreloader_parse_stack_from_content($content="") .  So the default stack is added to the array during wp_head action, while images preloaded using shortcodes will be filtered during the_content.

All the urls, are parsed using wpimgpreloader_parse_url($url) function, which also decides whether it should add the blog url at the beginning or not.

After parsing the all of the urls, what remains to do is to output the javascript code for image preloading. This action takes pace during the wp_footer by the wpimgpreloader_preload_js() function. If you take a look at it you’ll notice it first calls another function, wpimgpreloader_js_string_from_array($array, $derrive=false, $value=false) which returns a Javascript – formatted version of the input array.

How to use this plugin with your theme?

Course you can install the plugin and use it as it is, out-of-the-box. But, if you’d prefer you can also “embed” it’s functionality in your theme, and avoit having users to install it.  To do this, you need to add two more files  to your theme: wp-image-preloader-portable.php and jquery.imagepreloader.js, provided with the plugin. Then:
- enqueue the Javascript file (jquery.imagepreloader.js) to load in the header of your theme.
- include the PHP file (wp-image-preloader-portable.php) in your functions.php.
- call wpimgpreloader_enqueue($images) to add urls to your images into the stack. You can use as param a string (single url) or an array of urls, absolute or relative to the blog url.
- in the footer of your theme call wpimgpreloader_preload_js(TRUE) to print the image preloader Javascript.

Notes:

This plugin only generates the Javascript code to preload images. How fast the images are loaded is entirely the job of the web server serving the image(s).  Use it for images you may need later in the page (like hover) , not for images loaded anyway with the <img>  tag.
As the plugin name says it is for pre-loading images not for optimizing image(s) load.

Download:

You can always download the latest version from wordpress plugin’s page.

Questions?

If you need help,  have a question or suggestions, please add a comment to this article. Comments are held for moderation, but don’t worry I’ll try to get you an answer asap.

Marcat cu:
 

11 Comentarii la “WP Image Preloader”

  1. ren spune:

    this is brilliant! solved my super slow load time problem :] thanks!

  2. Andrew spune:

    Looks great!
    I am a newbie but I wonder if you can you enqueue several url using one shortcode like this:
    [preload=url1, url2, url3, ...]
    or you need to write
    [preload=url1]
    [preload=url2]
    [preload=url3]
    …?

    thank you

    • Adrian7 spune:

      Well, one piece of shortcode will preload a single image, so you’ll have to use
      [preload=url1]
      [preload=url2] …
      to preload more images.

      But, it’s a great idea for an update thanks!

  3. young spune:

    I have a problem:

    [preload=url1]
    [preload=url2] …

    in this way, the wordpress editor will add redundant , make the post too long with the blank, so i think whether [preload=url1, url2, url3, ...] is better, and if the preload function be added to wordpress media library to make preload images more easy, that will be amazing.

  4. [...] blog posts or in your wordpress themes, in menus slideshows etc. Find more about this plugin here: http://adrian.silimon.eu/wp-image-preloader/ [...]

  5. Can you add an option to automatically preload all images that are referenced in active theme’s style.css?

    • Adrian7 spune:

      Sorry, you comment was marked as spam, so I’ve approved it just now. Here’s your answer: Unfortunately not… . That would require to parse a css file, and that’s beyond the purpose of my plugin. But, you can add as many images you want in the “Default Stack”, on the Wp Image Preloader page under Plugins. It’s not automattically but you can identify what images need more time to load and put their urls there.

      Also you should avoid using very large images, no matter how fast your server delivers the content, the visitors with slow connections won’t get it.

  6. Gary spune:

    Causes error! to appear occasionally on reloading page. (think its when certain images don’t appear – maybe server error?)

    • Adrian7 spune:

      Hi Gary,

      There’s no way WP Image Preloader would show a dialogue box. Unless it’s conflicting another plugin – less likely, or you have a different plugin showing it.

      Can you describe me the steps to re-produce the error?

Adaugă comentariu