Skip to content

Instantly share code, notes, and snippets.

@AnadarProSvcs
Last active December 13, 2023 14:22
Show Gist options
  • Save AnadarProSvcs/cb47b70e7be13974722cd6bf6d1866f8 to your computer and use it in GitHub Desktop.
Save AnadarProSvcs/cb47b70e7be13974722cd6bf6d1866f8 to your computer and use it in GitHub Desktop.
This is a rough template for creating drag and drop/sortable lists in WordPress. This is very rough, but gives the frame work. The divs can be about as complex as you want them and created in any way appropriate. In a WordPress environment, you'd need to enqueue the javaScript and create the Ajax functions.
#sortable-area {
width: 100%;
min-height: 50px;
border: 1px solid #ccc;
padding: 10px;
}
.sortable-item {
margin: 10px;
padding: 5px;
border: 1px solid #666;
cursor: move; /* Cursor indicates item can be moved */
}
<div id="sortable-area">
<div class="sortable-item">Item 1</div>
<div class="sortable-item">Item 2</div>
<div class="sortable-item">Item 3</div>
<!-- Add more items as needed -->
</div>
jQuery(document).ready(function($) {
$("#sortable-area").sortable({
placeholder: "ui-state-highlight",
update: function(event, ui) {
// Optional: Add AJAX code here to update the order in the database
}
});
$("#sortable-area").disableSelection();
});
//Probably need to enqueue the jquery to allow the sorting. Also probably need to enqueue the javascript above.
function enqueue_custom_scripts() {
wp_enqueue_script('jquery-ui-sortable');
}
add_action('wp_enqueue_scripts', 'enqueue_custom_scripts');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment