Jquery Sortable lists using wordpress settings API
-
im trying to create 2 lists that i can sort data between, and hopefully get 2 arrays of the data. So far i have the lists working, and i can save the order of the data but im having trouble saving the data between the lists. Please help. Unsure how much code to paste here so im just gonna put it all.
here is my php<?php /** * Plugin Name: Test Plugin * Version: 1.0 */ class TestPlugin{ private static $instance; static function GetInstance() { if (!isset(self::$instance)) { self::$instance = new self(); } return self::$instance; } public function Initialize_Plugin() { add_action('admin_enqueue_scripts',array($this, 'admin_scripts')); add_action('admin_menu', array($this,'create_menu' )); } function admin_scripts() { if (is_admin()) { wp_register_script('admin-js', plugin_dir_url(__FILE__) . '/admin.js', array('jquery'), '', true); wp_enqueue_script('admin-js'); wp_enqueue_script('jquery-ui-core'); wp_enqueue_script( 'jquery-ui-sortable' ); } } function create_menu() { //create new top-level menu add_menu_page( 'TEST Options', 'TEST Options', 'administrator', 'test_options', array($this, 'plugin_options_page'), null, 99 ); //call register settings function add_action( 'admin_init',array($this, 'plugin_options') ); } function plugin_options() { register_setting( 'settings-group', 'filter_fields_order' ); } function plugin_options_page() { ?> <div class="wrap"> <h2>Theme Options</h2> <br/> <?php settings_errors(); ?> <form method="post" action="options.php" id="theme-options-form"> <?php settings_fields( 'settings-group' ); ?> <?php do_settings_sections( 'settings-group' ); ?> <?php $fields_order_default = array( 0 => array( 'id' => '0', 'name' => 'List Item 1', 'slug' => 'list_item_1' ), 1 => array( 'id' => '1', 'name' => 'List Item 2', 'slug' => 'list_item_2' ), 2 => array( 'id' => '2', 'name' => 'List Item 3', 'slug' => 'list_item_3' ), 3 => array( 'id' => '3', 'name' => 'List Item 4', 'slug' => 'list_item_4' ), 4 => array( 'id' => '4', 'name' => 'List Item 5', 'slug' => 'list_item_5' ), ); ?> <div class="admin-module"> <label><b>Sortable List</b> <em>(Drag & drop to rearrange order)</em></label> <ul style="display: flex;"> <ul style="display: block;"> <p>Group1</p> <ul id="sortable1" class="filter-fields-list" > <?php $filter_fields_order = get_option('filter_fields_order', $fields_order_default); foreach($filter_fields_order as $value) { ?> <?php if(isset($value['id'])) { $id = $value['id']; } if(isset($value['name'])) { $name = $value['name']; } if(isset($value['slug'])) { $slug = $value['slug']; } ?> <li class="sortable-item"> <?php echo $name; ?> <input type="hidden" name="filter_fields_order[<?php echo $id; ?>][id]" value="<?php echo $id; ?>" /> <input type="hidden" name="filter_fields_order[<?php echo $id; ?>][name]" value="<?php echo $name; ?>" /> <input type="hidden" name="filter_fields_order[<?php echo $id; ?>][slug]" value="<?php echo $slug; ?>" /> </li> <?php } ?> </ul> </ul> <ul style="display: block;"> <p>Group2</p> <ul id="sortable2" class="filter-fields-list"> <?php $filter_fields_order2 = get_option('filter_fields_order2', $fields_order_default2); foreach($filter_fields_order2 as $value) { ?> <?php if(isset($value['id'])) { $id = $value['id']; } if(isset($value['name'])) { $name = $value['name']; } if(isset($value['slug'])) { $slug = $value['slug']; } ?> <li class="sortable-item"> <?php echo $name; ?> <input type="hidden" name="filter_fields_order2[<?php echo $id; ?>][id]" value="<?php echo $id; ?>" /> <input type="hidden" name="filter_fields_order2[<?php echo $id; ?>][name]" value="<?php echo $name; ?>" /> <input type="hidden" name="filter_fields_order2[<?php echo $id; ?>][slug]" value="<?php echo $slug; ?>" /> </li> <?php } ?> </ul> </ul> </ul> </div> <?php submit_button(); ?> </form> </div> <?php } } $TestPlugin = TestPlugin::GetInstance(); $TestPlugin->Initialize_Plugin();
and i have admin.js
jQuery(document).ready(function($){ $(document).ready(function() { $('#theme-options-form').submit(function() { $(this).ajaxSubmit({ onLoading: $('.loader').show(), success: function(){ $('.loader').hide(); $('#save-result').fadeIn(); setTimeout(function() { $('#save-result').fadeOut('fast'); }, 2000); }, timeout: 5000 }); return false; }); }); $("#sortable1, #sortable2").sortable({ connectWith: ".filter-fields-list", }); $(document).ready(function () { $('#sortable1, #sortable2').sortable({ curosr: 'move' }); }); });
I feel like im close, but probably not. Thanks for your time.
I’m sorry about the incredibly poor format/syntax. It wasnt great at first but something happened when i pasted it in. It must be stabbing your eyes.
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Jquery Sortable lists using wordpress settings API’ is closed to new replies.