Add a drag-to-rank form to WordPress
Published Updated 3 min read
WordPress forms are built from text boxes, checkboxes and dropdowns. They are fine for names and messages, but none of them lets a visitor put a list in order, which is what you need when the question is 'rank these from most to least important' rather than 'pick one'.
The usual workaround is a numbered dropdown beside each option: rate this feature 1 to 5. It is awkward to fill in and the answers take work to interpret. A drag-to-rank list asks the same question in a way people understand at a glance, and the answer arrives as a readable ordered list.
A small shortcode does it without a form plugin. Add [drag_rank] to a post or page with a list of items, and visitors get a vertical list they reorder by dragging. When they submit, the ranked list lands in your inbox with their name and email.
Questions where the order is the answer
Feature prioritisation surveys, preference exercises, planning sessions where a group ranks options rather than choosing one: anywhere the relative order matters, a ranking says more than a scale or a checkbox.
A dropdown or radio button forces one choice and throws away the second and third preferences. A ranking captures what someone wants most and what they want next, in one submission.
If you need this on one or two pages, a full form builder for a single field type is more than the job needs. One PHP file, no database tables and no settings screen keeps the footprint small.
Four attributes and a list to drag
[drag_rank] takes items (a comma-separated list to rank, and the only required attribute), to (where results go, defaulting to the admin address in Settings), subject (defaulting to 'Ranking submission') and button (the submit label). A survey looks like [drag_rank items='Faster checkout, Better search, More product images, Email order updates' to='[email protected]' subject='Customer priorities survey'].
Visitors fill in name and email above the list, then drag each row by the handle at its start. Reordering uses the HTML5 Drag and Drop API, with no jQuery or other library.
Submitting sends an email through wp_mail with the person's name, email and the items in the order they left them, and the form is replaced by a confirmation message. Nothing is written to the database.
Phones, tallies and one question per form
HTML5 drag events do not fire on touch screens. Visitors on phones and tablets see the list but cannot reorder it. If most of your respondents are on mobile, a follow-up message can swap dragging for up and down arrow buttons on each row, which work everywhere.
Each response is one email and nothing is stored, so tallying results across many people is a spreadsheet job. After a few weeks the emails show what people consistently put first, but there is no report.
One shortcode is one ranking question. Two on the same page work independently and send separate emails, so give each its own subject to tell them apart; they cannot be combined into one submission.
What you get
Included
- Shortcode [drag_rank] with items, to, subject, and button attributes
- HTML5 Drag and Drop sorting with no JavaScript library or external dependency
- Name and email collection from the submitter
- Email delivery of the ranked list via wp_mail to a configurable address
- Confirmation message displayed on the same page after submission
Not included
- Database storage of results, one email per submission only
- Mobile touch-based dragging, HTML5 drag events do not fire on touchscreens
- Multiple ranking questions combined into a single form submission
- Aggregated results view or reporting dashboard in wp-admin
- Integration with Gravity Forms, WPForms, or Contact Form 7
Questions
I submitted the form but the email never arrived. Why?
WordPress sends mail through PHP's mail() by default, which many shared hosts block or route to spam. Install an SMTP plugin such as WP Mail SMTP, connect it to a sending service like Gmail or Mailgun, and test again. That is the cause in nearly every case where wp_mail seems to fail silently.
Can the starting order be shuffled on each page load?
Not in this build; items appear in the order you list them. Shuffling the items array in PHP before rendering would change that, and it removes anchoring bias, where people rank the first item higher simply because it comes first.
Build it in Steem
The button opens the builder with this guide's prompt filled in, so you can change anything before it runs. Nothing is built, and nothing is charged, until you send it.
Read the prompt (500 words)
Build a WordPress plugin called Drag to Rank Form. Plugin Name: Drag to Rank Form. Description: A [drag_rank] shortcode that lets visitors rank a list of items by dragging them into order and emails the result. Single PHP file. No external CSS or JavaScript libraries. Register a shortcode [drag_rank] with these attributes: items (comma-separated list, required), to (email address string, defaults to get_option('admin_email')), subject (string, defaults to 'Ranking submission'), button (string, defaults to 'Submit your ranking'). At the top of the shortcode function, generate a unique ID with uniqid('drr_') to namespace the list and hidden field so multiple shortcodes on the same page do not conflict. If the current URL contains the query parameter drag_rank_success=1, output a <div style='padding:12px;border-left:4px solid #46b450;background:#ecf7ed;margin-bottom:16px;'> with the text 'Thank you. Your ranking has been submitted.' and return early, showing no form. Otherwise, render a <form method='post' action='<?php echo esc_url(admin_url("admin-post.php")); ?>'>. Include: a hidden input name='action' value='drag_rank_submit'; wp_nonce_field('drag_rank_submit','drag_rank_nonce'); a hidden input name='drag_rank_to' value of the escaped to attribute; a hidden input name='drag_rank_redirect' value of get_permalink() escaped; a hidden input name='drag_rank_order' with an id of the unique id plus '_order'. Render a name field and an email field, each required, with max-width 400px and display block. Render a <ul> with the unique id as its HTML id, list-style none, max-width 480px. For each item from the items attribute (trim each one), output a <li> with draggable='true', a data-value attribute of the item text, inline styles for padding 10px 12px, border 1px solid #ddd, margin-bottom 4px, background white, cursor grab, display flex, align-items center, gap 10px, border-radius 3px. Inside the li, a span with color #aaa showing the hamburger character (☰) as a handle, followed by the item text. Render a submit button styled with background #0073aa, white text, no border, padding 8px 18px, border-radius 3px. Add an inline <script> block wrapped in an IIFE. Select the ul by its unique id and the hidden order input by the unique id plus '_order'. Track a dragging variable. Add event listeners on the ul for dragstart (set dragging to event.target.closest('li')), dragover (preventDefault, highlight the target li with outline 2px dashed #0073aa), dragleave (remove the outline from the target li), and drop (preventDefault, remove outline, reorder by inserting dragging before or after the target li based on whether clientY is above or below the target midpoint). On the form submit event, serialize the current li data-value attributes into the hidden order input as a JSON array; if empty, preventDefault and alert the user to drag items first. Register admin_post_drag_rank_submit and admin_post_nopriv_drag_rank_submit handlers pointing to the same function. In the handler: check_admin_referer('drag_rank_submit','drag_rank_nonce'). Sanitize name, email, and to_address from POST (falling back to admin_email for to_address). json_decode the order field; if not a non-empty array, wp_die. Sanitize each item with sanitize_text_field. Get subject from POST or default to 'Ranking submission'. Build email body with name, email, and numbered ranking list. wp_mail to to_address. add_query_arg drag_rank_success=1 to the redirect URL from POST. wp_safe_redirect and exit. No database writes. No external HTTP requests.
Steem