我的错误比我想象的要多,但我能找出我需要什么。我完全删除了wp_localize_script()
这不是必需的(这是用于“管理”选项卡)。我补充道include \'my-ajax.php\';
到主插件。php修复了Jquery,最终得到
include \'my-ajax.php\';
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\' );
wp_register_style( \'test_jquery\', \'//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css\', false, \'1.0.0\' );
wp_enqueue_style( \'test_jquery\' );
}
}
add_action(\'admin_enqueue_scripts\',array($this, \'admin_scripts\'));
管理员。js公司
jQuery(document).ready(function($){
$(\'#sortable4, #sortable3\').sortable({
connectWith: ".filter-fields-list",
update: function (event, ui) {
var list = this.id;
var order = $(this).sortable(\'toArray\').toString();
args = {
url: ajaxurl,
type: \'POST\',
async: true,
cache: false,
dataType: \'json\',
data:{
action: \'item_sort\',
order: order,
list: list
},
success: function(response) {
//alert(list+order);
return;
},
error: function(xhr,textStatus,e) {
alert(e);
return;
}
};
$.ajax(args);
}
}).disableSelection();
});
在我的ajax中。php
<?php
function my_save_item_order() {
$order = explode(\',\', $_POST[\'order\']);
$list = $_POST[\'list\'];
if($list == "sortable3"){
update_option(\'filter_fields_order\', $order);
}
if($list == "sortable4"){
update_option(\'filter_fields_order2\', $order);
}
wp_die();
}
add_action(\'wp_ajax_item_sort\', \'my_save_item_order\');