如何制作基于主题选项ajax的单词? 在word-press中,存储主题选项刷新页面,我想让它基于ajax。我面临的问题是,没有add_option或update_option的代码,我怎么能让它基于ajax谢谢
答案 0 :(得分:0)
为什么你要重新发明轮子?这个有很多东西
您可以在互联网上找到更多内容。但如果您真的想在ajax主题选项上进行制作,那么您需要使用wp_ajax_(action)
<强>使用Javascript:强>
jQuery(document).ready(function($) {
// We'll pass this variable to the PHP function example_ajax_request
var fruit = 'Banana';
// This does the ajax request
$.ajax({
url: ajaxurl,
data: {
'action':'example_ajax_request',
'fruit' : fruit
},
success:function(data) {
// This outputs the result of the ajax request
console.log(data);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
});
<强> PHP 强>
function example_ajax_request() {
// The $_REQUEST contains all the data sent via ajax
if ( isset($_REQUEST) ) {
$fruit = $_REQUEST['fruit'];
// Let's take the data that was sent and do something with it
if ( $fruit == 'Banana' ) {
$fruit = 'Apple';
}
// Now we'll return it to the javascript function
// Anything outputted will be returned in the response
echo $fruit;
// If you're debugging, it might be useful to see what was sent in the $_REQUEST
// print_r($_REQUEST);
}
// Always die in functions echoing ajax content
die();
}
关于ajax的更多解释,你可以找到它here
答案 1 :(得分:0)
This worked for me
$(document).on("submit", "form[action='options.php']", function(event) {
var btn = $(document.activeElement);
name = btn.attr("name");
if(name == "update")
{
event.preventDefault();
var settings = $(this).serialize();
$.post( 'options.php', settings ).error(
function() {
alert('error');
}).success( function() {
alert('options saved successfully');
});
return false;
}
});