How do I include drag-and-drop in a plugin?

I want to build a WordPress plugin that will allow users to add and delete fields from a custom post type. I want them to be able to do this with drag-and-drop, just like they add widgets to a sidebar. That is to say, the plugin will have a group of allowable fields, and the users will drag and drop those fields into a box to allow them to configure the fields in a custom post type.

Are there WordPress functions that allow developers to do this? Is there a tutorial anywhere that shows how to do this?

Thanks!

1 Answer
1

WordPress has a few of these libraries that come with core, you can view the list here: Default Scripts Included and Registered by WordPress. What you’re looking for is Draggable, Sortable, and in your case Droppable.

You would use wp_enqueue_script() to add it like so:

wp_enqueue_script( 'jquery-ui-droppable' );

WordPress registers these scripts using a Handle which you can use to add them, the list of Script Handles can be found in the first link above. You would still need to hook into the admin panel and add the script:

function load_custom_wp_admin_style() {
        wp_enqueue_script( 'jquery-ui-droppable' );
}
add_action( 'admin_enqueue_scripts', 'load_custom_wp_admin_style' );

Leave a Comment