我在编辑后的页面(管理端)上有一个链接,可以调用AJAX和jQuery在我的functions.php
页链接已连接并调用jQuery,但我似乎无法调试从jQuery调用的函数内部发生的事情。我希望单击的链接删除帖子的自定义元数据(这是一种自定义帖子类型),然后删除一个文件。
下面是末尾带有删除功能的代码:
//Add AJAX functionality to post.php to delete files
add_action(\'admin_enqueue_scripts\', \'my_admin_enqueue_scripts\');
add_action(\'wp_ajax_delete_meta\', \'delete_pdf_and_metadata\');
//Add my custom JS to the header of admin
function my_admin_enqueue_scripts($hook) {
global $current_screen;
if ( \'post.php\' != $hook )
return;
wp_register_script(\'my-scripts\', get_template_directory_uri() . \'/js/custom/my-scripts.js\' );
wp_enqueue_script(\'my-scripts\');
wp_localize_script(\'my-scripts\', \'wp_ajax\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' )));
}
function delete_pdf_and_metadata() {
global $post;
//delete metadata
$the_id = intval($_POST[\'the_id\'] );
$the_pdf = get_post_meta($post->ID, $the_id);
delete_post_meta($post->ID, $the_id, $the_pdf[\'name\']);
//TODO Delete PDF
}
以下是jQuery调用:jQuery(document).ready(function($) {
$(\'.delete_pdf\').each(function(i,e) { //grab the class delete-pdf
var id = $(this).attr(\'id\').replace(/delete-/, \'\');
var li = $(this).closest(\'li\');
$(this).click(function(){
$.post(ajaxurl, { action: \'delete_meta\', the_id: id }, function(data){
return id;
});
});
});
});
使用FireBug时,我看到的是响应为0。调试函数内部发生的事情的最佳方法是什么delete_pdf_and_metadata()
通过jQuery调用?谢谢