您要做的是定义一个回调函数来处理您的请求,并执行需要挂接的操作:
wp_ajax_(action)
...其中“(action)”是需要在AJAX请求中提供的“action”参数的名称(稍后将详细介绍)。
首先,在PHP逻辑中定义回调函数,让我们使用unc_gallery_upload
.
在本例中,“unc\\u gallery\\u upload”是您操作的名称,因此在定义挂钩时,您可以将wp_ajax_
具有unc_gallery_upload
, 然后你得到wp_ajax_unc_gallery_upload
, 这是你的动作钩的名字。
PHP
function unc_gallery_upload_handler() {
//handle request e.g. var_dump($_POST[\'data\']);
}
add_action( \'wp_ajax_unc_gallery_upload\', \'unc_gallery_upload_handler\' );
AJAX请求中的JavaScript(JS代码)
var options = {
//this will post to example.com/wp-admin/admin-ajax.php
//the ajaxurl variable is already defined in the global
//scope of the WordPress dashboard
url: ajaxurl,
//your payload, accessible on $_POST[\'data\']
data: {
//this name of your action as defined in your wp_ajax_(action)
//hook that will fire your
action: \'unc_gallery_upload\',
payload: \'somedata\'
},
target: \'#targetLayer\',
beforeSubmit: beforeSubmit,
uploadProgress: uploadProgress,
resetForm: true,
complete: complete
};
$(\'#uploadForm\').submit(function() {
$(this).ajaxSubmit(options);
return false;
});
我强烈建议阅读:
wp_ajax_(action)注意:由于您处于管理区域,因此不想定义wp_ajax_nopriv_(action)
.