cre*_*ity 5 php ajax wordpress jquery
我写了一个wordpress插件女巫在我的模板中添加了一些评论功能.通过ajax,所有的东西都应该传输到wordpress数据库.
问题是 - ajax处理程序需要一个php文件来捕获查询
if(isset($_POST['name'], $_POST['title'], $_POST['description'])) {
// do something with wordpress actions, e.g. get_current_user, $wpdb
}
Run Code Online (Sandbox Code Playgroud)
在用户传输查询时,ajax处理程序调用php文件,如下所示:
$('#ajax_form').bind('submit', function() {
var form = $('#ajax_form');
var data = form.serialize();
$.post('../wp-content/plugins/test/getvars.php', data, function(response) {
alert(response);
});
return false;
Run Code Online (Sandbox Code Playgroud)
该getvars.php不知道WordPress的环境,因为它是由用户直接调用提交,我想补充的WordPress的环境类,包括是不是好作风.
还有其他方法吗?感谢你的支持.
是的使用builtin wordpress ajax动作:
你的jquery看起来像这样:
$('#ajax_form').bind('submit', function() {
var form = $('#ajax_form');
var data = form.serialize();
data.action = 'MyPlugin_GetVars'
$.post('/wp-admin/admin-ajax.php', data, function(response) {
alert(response);
});
return false;
Run Code Online (Sandbox Code Playgroud)
你的插件代码如下:
add_action("wp_ajax_MyPlugin_GetVars", "MyPlugin_GetVars");
add_action("wp_ajax_nopriv_MyPlugin_GetVars", "MyPlugin_GetVars");
function MyPlugin_GetVars(){
global $wpdb;
// use $wpdb to do your inserting
//Do your ajax stuff here
// You could do include('/wp-content/plugins/test/getvars.php') but you should
// just avoid that and move the code into this function
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5264 次 |
最近记录: |