在ajax中从wordpress调用PHP函数? [英] Calling PHP function from wordpress in ajax?
问题描述
reset.php文件:
<?php
add_action('admin_init','popup_template_reset_options');
function popup_template_reset_options()
{
delete_option('popup_template_on');
delete_option('popup_template_close');
delete_option('popup_template_escape');
delete_option('popup_template_external');
delete_option('popup_template_home_page');
delete_option('popup_template_all_pages');
delete_option('popup_template_template');
delete_option('popup_cookies_display_after_like');
add_option('popup_cookies_display_after_like','365');
//add_option('popup_template_on','1');
add_option('popup_template_close','1');
add_option('popup_template_escape','1');
add_option('popup_template_external','1');
add_option('popup_template_force_timer','2');
add_option('popup_template_home_page','1');
add_option('popup_template_all_pages','1');
add_option('popup_template_template','2');
}
?>
脚本Ajax:
<script type="text/javascript">
$(document).ready(function() {
$('#reset_general').click(function()
{
$('#result1').css("display", "block");
jQuery('#result1').animate({'opacity': '1'});
});
});
function resetgeneral() {
$.ajax({type: 'POST', url: '<?php echo WP_PLUGIN_URL; ?>/fantasticpopuptemplate/inc/reset.php', success: function(response) {
//$('#fff').find('.form_result').html(response);
$('#result1').css("display", "none");
$('#resets1').css("display", "block");
$('#resets1').html("Settings Resets");
$('#resets1').fadeOut(2500, "linear");
}});
return false;
}
</script>
<form onsubmit="return resetgeneral();" id="form_general_reset" class="form-1">
<input type="submit" value="Reset" id="reset_general" name="reset" class="button-secondary"/>
<input name="action" type="hidden" value="reset" />
</form>
我试图在ajax中调用php reset函数,但是当我谷歌搜索时,我知道没有直接调用php函数的可能性,因此我将该特定函数放在单独的php文件中并调用该php文件,我不确定我怎么能在ajax中做到这一点,我尝试了上面的代码,但是没有任何反应.出现设置重设消息.我该怎么做任何帮助都将非常有用.在使用ajax概念之前,我尝试使用php中的isset
函数,但是每次它都会导致页面加载,只有我跳进了ajax.
Hi i trying to call the php reset function in ajax but when i googling i know that not possibilities of direct calling php function so I put that particular function in seperate php file and call that php file i am not sure how can i do this in ajax i tried this above code but nothing happen. Settings Resets Message appear. How Can I do this Any Help would be great. Before using the ajax concept i tried with isset
function in php But it getting page load every time for that only i jump into the ajax.
推荐答案
有一种通过admin_ajax.php在WordPress插件中实现AJAX的标准方法.通过将功能分配给AJAX操作,该文件名具有误导性,因为它也可以在前端使用.
There is a standard way of achieving AJAX in WordPress plugin by relying on the admin_ajax.php. The file name is misleading, as it can be used in the frontend too, by assigning functions to AJAX actions.
在WordPress Codex上有一个很好的描述:
There is a good description on the WordPress Codex:
http://codex.wordpress.org/AJAX_in_Plugins
要注意的一件事:您的AJAX处理程序函数将始终必须使用die()
命令终止,以避免WordPress产生额外的"0"输出.
One thing to be aware of: Your AJAX handler functions will always have to terminate with a die()
command, to avoid the extra '0' output from WordPress.
这篇关于在ajax中从wordpress调用PHP函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!