php 在ajax中从wordpress调用PHP函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16954945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling PHP function from wordpress in ajax?
提问by Vignesh Pichamani
reset.php file:
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');
}
?>
Script 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>
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.
嗨,我试图在 ajax 中调用 php reset 函数,但是当我使用谷歌搜索时,我知道直接调用 php 函数的可能性不大,所以我将该特定函数放在单独的 php 文件中并调用该 php 文件,我不知道如何在ajax 我试过上面的代码,但没有任何反应。出现设置重置消息。我该怎么做 任何帮助都会很棒。在使用 ajax 概念之前,我尝试isset
在 php 中使用函数,但每次都会加载页面,只有我跳入 ajax。
回答by beerwin
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.
有一种标准的方式可以通过 admin_ajax.php 在 WordPress 插件中实现 AJAX。文件名具有误导性,因为它也可以通过将函数分配给 AJAX 操作在前端使用。
There is a good description on the WordPress Codex:
WordPress Codex 上有很好的描述:
http://codex.wordpress.org/AJAX_in_Plugins
http://codex.wordpress.org/AJAX_in_Plugins
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 处理程序函数将始终必须以die()
命令终止,以避免来自 WordPress 的额外“0”输出。