Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Drag and Drop Order for Plugin Options

Posted on February 21, 2012 by Pippin in Action and Filter Hooks, Advanced, Ajax, Member Restricted, Subscriber Only, Tutorials, WordPress Admin / Dashboard, Working with jQuery, Writing Plugins 53 Comments
Home» Tutorials » Action and Filter Hooks » Drag and Drop Order for Plugin Options
Tweet
Love It - 4

The drag and drop method of ordering items in an option table is very popular, and also extremely useful. Rather than forcing users to manually enter “order” numbers to sort items the way they want, they can simply click and drag the items into the desired order. I use drag-and-drop ordering quite a bit in my plugins, and would now like to show you a simple example of how you can do it too.

To start, I have created a very simple plugin that does little more than setup a plugin option, user add_option(), and then outputs the contents of the option into a table on the plugin settings page. The idea is that each row in the table should be draggable, so that the user can define the order of the items in the table. So by clicking and dragging, the user can place the initially fifth item at the very top, or anywhere else he or she so desires.

Enabling drag-and-drop on the table is done with the jQuery UI Sortable library, which can be loaded into WordPres with wp_enqueue_script(‘jquery-ui-sortable’).

Saving the new order of the plugin options is done via ajax, using the wp_ajax_{action{ hook.

In the case that you have trouble following the above video, check out these tutorials:

  • Writing Your WordPress Plugin
  • Processing Ajax Requests in WordPress plugins

Update

As pointed out by Thomas in the comments, I have not included a nonce check to verify that the ajax processing function is only ran when it is supposed to. For an extra measure of security, and in ALL actual implementation of this code, you should use the wp_create_nonce() and check_ajax_referer() to ensure that everything is secure.

Update 2

In order to fix a bug where the $dad_list options were created by default upon activation, I’ve updated the download files to include a check for !is_array($dad_list) in the activation function.


If you are a premium, then you can download the source code for this tutorial below. The download includes both the starting point plugin (that I started with in the video) and also the finished plugin.

Check out the list of benefits you receive by registering, or join now!

Tweet Follow @pippinsplugins
add_option, add_options_page, Ajax, register_setting, sortable, update_option

53 comments on “Drag and Drop Order for Plugin Options”

  1. Thomas Griffin says:
    February 21, 2012 at 1:59 pm

    Good tutorial, Pippin! But where is nonce and check_ajax_referer check? Not that anything in this tutorial will trigger an unwanted move/save, but you can never assume upon a user that same grace when they go out on their own. :-)

    Reply
    • Pippin says:
      February 21, 2012 at 2:04 pm

      That is very, very true! I’ll add in a note about that.

  2. Zach says:
    February 21, 2012 at 3:19 pm

    Not that I’m too lazy to watch the entire video or anything ;) but does this address drag ‘n drop ordering for things like the posts, pages, custom post type tables? If not, make it into a plugin and you’ve got yourself a buyer :)

    Reply
    • Pippin says:
      February 21, 2012 at 3:30 pm

      It does not exactly address that, but the techniques are applicable to that situation. The only real difference is the save method. I would make a plugin to drag and drop post order, but there’s really no need, since there are already half a dozen plugins out there that do that.

    • Zach says:
      February 21, 2012 at 3:51 pm

      Anything better than http://wordpress.org/extend/plugins/easy-post-order/ ? That uses a separate screen for re-ordering and haven’t found anything that actually works “right there” in the list. Thanks!

    • Pippin says:
      February 21, 2012 at 3:55 pm

      My favorite is Post Types Reorder.

    • Zach says:
      February 21, 2012 at 3:56 pm

      Appreciate that – looks like it uses a separate “Re-order” page so I think I need to use the good ‘ol lesson of “if you are not happy with what’s out there, build it yourself” :)

    • Pippin says:
      February 21, 2012 at 4:00 pm

      Yep, pretty much. The only one I know of that reorders on the post list is Simple Page Ordering, but it only works for Pages, not Posts.

  3. CoreyCapetillo says:
    February 22, 2012 at 6:23 pm

    I went in to install the plugin and I was provided with the following error:
    Warning: Invalid argument supplied for foreach() in /Users/user/Sites/wordpress/wp-content/plugins/drag-and-drop-plugin-sample/includes/admin-page.php on line 28

    Other than that, your previous tutorials have been flawless and thorough. Keep ‘em comin’!

    Reply
    • CoreyCapetillo says:
      February 22, 2012 at 6:26 pm

      I should note that the plugin I attempted to install was the sample and the error provided was encountered on the Settings > Drag and Drop page. The content that’s supposed to pre-populate the table failed to… can’t think of the word.

    • Pippin says:
      February 23, 2012 at 9:23 am

      By sample, do you mean it was the unfinished version, or the finished version?

  4. kristopherk says:
    March 3, 2012 at 3:40 pm

    Great tutorial! I uploaded the “complete” version and am getting the same error as mentioned above…

    Warning: Invalid argument supplied for foreach() in

    Reply
    • kristopherk says:
      March 3, 2012 at 6:23 pm

      I was able to get it to populate by adding the following code in the admin-page.php – so the initial arrary setup is not working.

      $list = array(
      ‘Option one’,
      ‘Option two’,
      ‘Option three’,
      ‘Option four’,
      ‘Option five’,
      ‘Option six’,
      );
      update_option(‘dad_list’, $list);

    • Pippin says:
      March 4, 2012 at 10:32 am

      I will check it out while in the office tomorrow. Once fixed, I’ll update the complete plugin.

    • Pippin says:
      March 5, 2012 at 10:21 am

      I’ve just updated the download file and tutorial to fix the problem. I needed to add a !is_array($dad_list) to the activation hook.

  5. yellowhousedesign says:
    May 10, 2012 at 11:35 am

    Hey Pippin,
    Been bangin my head against the wall with this one for a while — the difference between your setup and mine is that I’m trying to add this to existing plugin options (so what I’m thinking is that I’d need to create an option is an array, within the array of other options) — which may be where I’m going wrong here — created a PasteBin http://pastebin.com/PHhwnK5R

    Appreciate any help!

    Reply
    • yellowhousedesign says:
      May 10, 2012 at 11:47 am

      Just another quick tid-bit — this only happens after I save the plugins page once. I know this particular part doesn’t need the save action — but this drag ‘n drop is in the list of other plugin settings. After first plugin activation and page load — works fine, after save, it doesn’t work. I also dragged one of the options and then resaved, but it did not effect the order. Thanks!

    • Pippin says:
      May 11, 2012 at 11:11 am

      Save it and you see the problem. Does the problem go away when you reload the page? And did the settings actually save?

  6. yellowhousedesign says:
    May 11, 2012 at 11:20 am

    Hi Pippin,
    I’ve been going full-force on this one since I knew you’d be a bit busy yesterday ;) I continued my question on Stack Exchange http://wordpress.stackexchange.com/questions/51802/update-option-stored-in-multi-dimensional-array which then lead to http://wordpress.stackexchange.com/questions/51889/plugin-settings-not-saving-on-ajax-re-ordered-table The basic idea here is that I shouldn’t be using AJAX to update the options on a page that has other settings (where AJAX is not used, just a regular save button). I hope it’s not too cryptic, but if you do have a few minutes and want to jump in on Stephen and I’s convo, I’d really appreciate it. At this point, it’s a matter of figuring out how to still use the draggable interface that syncs the order on page-save instead of AJAX. Thanks!

    Reply
    • Pippin says:
      May 12, 2012 at 9:36 pm

      Hey man, sorry, I haven’t had a chance to take a look yet. Have you had any luck yet?

    • yellowhousedesign says:
      May 13, 2012 at 5:42 am

      Hey Pippin,
      No worries – I haven’t made any headway as of yet on that. My latest response in that second question link is where I’m currently at. Hope everything is going well with the baby so far! Thanks!

    • yellowhousedesign says:
      May 14, 2012 at 9:53 am

      Hey Pippin,
      Ended up getting this one working by myself – thanks again for checking in.

    • Pippin says:
      May 14, 2012 at 10:44 am

      Oh nice. I was about to take another look at those questions, but apparently you don’t need me :D

  7. jgalea says:
    September 28, 2012 at 4:39 pm

    Hey Pippin,

    Does it work any differently if I want to reorder list items within a widget settings box in the dashboard under Appearance > Widgets?

    Reply
    • Pippin says:
      September 28, 2012 at 6:25 pm

      Nope, same concept :) The only real difference is the way you save the options. Instead of saving to wp_options you will save to the widget option.

  8. jgalea says:
    September 30, 2012 at 3:39 am

    Thanks!

    Reply
  9. Usman says:
    January 29, 2013 at 10:50 am

    Useless tutorial ,If you cant share your knowledge and source code then dont upload such videos.You guys just need money nothing else..

    Reply
    • Pippin says:
      January 29, 2013 at 11:02 am

      Nice try. Look at the huge number of free tutorials / plugins I’ve written and then rethink your statement.

    • Usman says:
      January 29, 2013 at 2:15 pm

      Dear this is the marketing techniquie not your courtesy.First you offer free stuff and then charge for rest of the stuff… Even the video you uploaded is fade.Give me your moneybooker and I am sending you payment. The reason I am fed up from you people is that you are using knowledge as weapon to earn money.Go and search about Wikipedia,Stackoverflow.If you have knowledge about something,then share it or keep it to yourself.Dont make offers like premium etc etc..

    • Pippin says:
      January 29, 2013 at 2:34 pm

      Knowledge isn’t always free and I have a family to feed.

      Let me link you to just a few of the many, many things I give a way for free, all of which are ways that I “share my knowledge”:

      31 plugins released for free on WordPress.org.
      105 answers given for free on the WordPress Stack Exchange.
      111 completely free tutorials posted on this site (only 34 require membership).
      151 replies created on the bbPress support forum (someone else’s plugin) for free.
      10 PAGES of replies to support tickets on WordPress.org.
      Nearly 4000 replies posted on support tickets for the FREE Easy Digital Downloads plugin

      So, is asking for a $6 one time payment really all that terrible to ask for in order to gain access to just a micro percentage of the material I’m releasing? Feel free to think so.

    • Justin Sainton says:
      January 29, 2013 at 2:35 pm

      *drops mic*

    • Usman says:
      January 29, 2013 at 2:48 pm

      Thank you for all that..Atlast you admit that you have family to feed and these things aren’t free.You uploaded this stuff and created this website not to HELP us but to feedb your family. This is your main goal

    • Pippin says:
      January 29, 2013 at 2:50 pm

      You can read about the true goals of this site on the About page: http://pippinsplugins.com/about/

  10. Keith says:
    January 29, 2013 at 2:36 pm

    Don’t feed him…

    Reply
  11. Carlitoescobar says:
    January 29, 2013 at 2:40 pm

    who is he calling “you people”? Racist

    Reply
    • Usman says:
      January 29, 2013 at 2:49 pm

      The world knows who is racist my brother.
      Neing a muslim I dont want to induldge and create a new debate here.

    • Pippin says:
      January 29, 2013 at 2:50 pm

      Keep it nice guys.

  12. John Turner says:
    January 29, 2013 at 2:42 pm

    Usman you have no idea what you are talking about. Pippin gives back to the WordPress community in so many way more than what he has listed. What have you done? You my friend are just a blood sucking leech, a taker and a troll.

    Reply
    • Usman says:
      January 29, 2013 at 2:51 pm

      Well I also do free answers to community on stackover flow.To feed my family ,I do freelancing and a job.

    • Pippin says:
      January 29, 2013 at 2:53 pm

      Usman, something you need to realize is that this is my job. I don’t get paid from an employer.

    • Usman says:
      January 29, 2013 at 2:58 pm

      Dear you can do freelancing OR open a school for wordpress training.The way you are spreading knowledge is just like “If a student is enrolled in a school ,he got 2 classes and for the third class,he would asked to pay a handsome amount” .
      Dear there are lot other ways of earning,through Ads. See stackoverflow peoplewith over 50K reputation are available there just to answer other questions because they feel pleasure in serving others and sharing their knowledge.
      I would have no problem to become the premium member of this site.My country doesn’t support paypal ,that’s why i have to rely on moneybooker.Your site doesn’t contain moneybooker.
      I was just going through yout “Faded” video and Got the message that source code is for premium members. WoW

    • Pippin says:
      January 29, 2013 at 3:01 pm

      You are free to think what you want but I assure you that I am doing nothing wrong.

      You are welcome to pay via credit card, not just PayPal.

  13. Usman says:
    January 29, 2013 at 3:02 pm

    Thank you so much :)
    BR

    Reply
  14. Chris Kelley says:
    January 29, 2013 at 3:07 pm

    hmm isn’t he training people for free? You do know schools don’t give you the source code when they teach, They teach the methods and then tell you to code something specifically if you cant you fail. At least that what happened when I got my degree in development.

    Reply
  15. jonathan says:
    January 29, 2013 at 3:09 pm

    Hey @Usman… way to give feedback, I’m sure if you keep complaining about all the free stuff Pippin offers he’ll just agree and live in a box while you sponge off his knowledge that has taken years to establish.

    Learning costs money, one way or another, hence colleges, universities, tutorial sites that charge.. oh yeah, books also, forgot about those thousand year old learning devices that those publishing bastards charge for, how dare they!

    Grow up and move along.

    Reply
    • Usman says:
      January 29, 2013 at 3:19 pm

      Brother those people have a sole goal of earning money.They tell you that Yes we are offering this for this amount.Tell me one Book that was given to you by your teacher and he told you that “first 50 pages are free and for the rest of pages you need to pay me..” Is that true? No.
      You are talking about devices ,Abacus was the first calculating device,Was that offering you addition only and for subtraction you need to pay ? Isn’t it?
      I dont want to go into details of all that.THINGS WILL KEEP MOVING AS THEY ARE NOW AND THEY WILL NOT CHANGE JUST BECAUSE OF ME.I just want to put forward what I felt.No hard feelings for anyone. (My last reply)
      BR

  16. Erik says:
    January 29, 2013 at 3:13 pm

    Dear Usman,
    How much time do you think Pippin has spent acquiring the knowledge he has. Contrary to what you might believe, time = money in the grown up world. I think he deserves a slight return on that investment. I’m amazed Pippin finds the time to do so much work for FREE. Time which otherwise could be spent making money.
    Erik

    Reply
  17. Usman says:
    January 29, 2013 at 3:25 pm

    brother go and google about BalusC in java.Go and see his contributions on Stackoverflow.
    with reputation of 356K.can you believe this?
    nothing hidden,,All free,,, Honestly I wish to donate BalusC some reward for his contribution because I like the way he spread knowledge.

    Reply
    • Erik says:
      January 29, 2013 at 3:31 pm

      If you don’t like it, don’t use it my friend!

  18. Phil Johnston says:
    January 29, 2013 at 3:28 pm

    Usman,

    The only time you or anyone has a right to information is if it is publicly funded. For example: government information is funded by the taxpayers and should be available to the taxpayers.

    But you are not entitled to all information that everyone has created. If they decide to give it to you for free, than that is their choice and you should be thankful for it. But if they do not decide to give their information to you for free, you are not entitled to it.

    Pippin does a lot of work compiling helpful information for people and he can share it however he pleases. If paying for that information allows him to spend more time creating helpful things for the community, I am more than willing to pay for it.

    I’ve learned more from Pippin in a few conversations than I have from 2 years of school at an official college – which cost me thousands of dollars.

    Reply
  19. Gregg says:
    May 6, 2013 at 12:05 am

    Thank you!

    Reply
  20. Ayoub says:
    May 6, 2013 at 4:06 pm

    Hi man thanks for the tuts , have a question
    let’s say i wanna apply this to make a theme option page . so i organize my boxs order like you showed , but what i need to display this in home page ( mean the order of boxs & boxs html codes )
    front-office ^^ thanks again

    Reply
    • Pippin says:
      May 7, 2013 at 4:44 pm

      That goes beyond the scope of this tutorial, sorry.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

  • Using Ajax in Your Plugin and WordPress Admin
  • Create a Live Search in WordPress with jQuery and Ajax
  • WordPress Rewrite API – Part 3
  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes
  • Plugin Development 101 – Registering a Custom Post Type
  • Plugin Development 101 – Intro to Actions

Latest Tutorials

  • Submitting Your First Pull Request to a WordPress Plugin on Github (2)

    Github is an extremely popular tool for managing WordPress plugins, and one...

  • Plugin Development 101 – Introduction to Adding Dashboard Menus (1)

    Adding new menus, both top level and sub level, to the WordPress Dashboard is a really common task for plugins...

  • Plugin Development 101 – Intro to Loading Scripts and Styles (16)

    In this part of Plugin...

Enter your email to receive automated updates when new posts are published

Latest Tweets

  • Shipped some serious code tonight, and now to hit the sack. https://t.co/1V68FVHYWg
    May 21, 2013
  • @tnorthcutt https://t.co/AuWTLzmqOY
    May 21, 2013
  • @tnorthcutt Figured it out, thanks
    May 21, 2013

Topics

campaign monitor shortcodes Rémi Corson add_options_page the_content Sugar Event Calendar featured get_user_meta contextual help add_shortcode attachments Tom McFarlin wp_enqueue_script short codes mail chimp authors Related posts attachment plugin image login forms do_action recent posts post types bbpress apply_filters comments short code taxonomies custom post type images Ajax gallery Stripe taxonomy jquery widgets users add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2011 Pippin's Plugins