WordPress Code Snippets

A fresh mix of code snippets and tutorials

jQuery Add Another Image Link


This code snippet is taken from my plugin, User Submitted Posts. It displays a link that users may click to add another file-upload input field.

Step 1: The Markup

Here is the basic HTML that we’ll need for the script to work:

This creates two hidden fields that provide the following information:

  • The image input limit, provided by usp-image-limit
  • The current image count, provided by usp-image-count

The form may include whatever other fields and/or markup is required, but must include these hidden fields. Additionally, the form must include the “add another image” link and a file-input field. For example:

This gives us the container <div> that will contain the cloned file inputs. It also provides the original file input (used for cloning), and the “add another image” link. All of these factors come into play in the next step.

Step 2: The JavaScript

With the HTML included in your form, add the following jQuery function to enable the “add another image” functionality:

No editing is required, this should work out of the box. Basically what we’re doing here is grabbing the image limit and count data, and then cloning the file input field whenever the #usp_add-another link is clicked. When the file limit is reached, the add-another link will be hidden from the user.

Note that if you use this technique it is important to include proper server-side validation and file checking (i.e., don’t rely on JavaScript as it can be disabled by the user).

To see this script in action, check out User Submitted Posts or USP Pro!

Show Support

Like our new Facebook Page to show support!