我正在尝试在中创建按钮edit-comments
表,我想使用Ajax和一个插件来完成这项任务。
这是插件/无电子邮件/无电子邮件下插件的代码。php:
<?php
/*
Plugin Name: Approve Comment but don\'t send email
Plugin URI: ""
Description: Approve commnet or Trash Commnet but don\'t send the email.
Author: MNTS
Version: 1.1
Text Domain: delete_comment
Author URI: http://www.mnts.com/
*/
add_action(\'wp_ajax_deteletcomment\', \'deteletcomment\');
add_action(\'wp_ajax_nopriv_deteletcomment\', \'deteletcomment\');
function deteletcomment() {
echo "test";
exit;
}
add_action(\'wp_ajax_approvecomment\', \'approvecomment\');
add_action(\'wp_ajax_nopriv_approvecomment\', \'approvecomment\');
function approvecomment() {
echo "test";
exit;
}
这是主题/themes/the\\u current\\u主题/functions下的客户端代码。php:
function addCommentColumns($columns)
{
?> <script>
function commentdel(id) {
console.log(\'id: \' + id);
var data = {
\'action\': \'deteletcomment\',
\'comment_id\': id
};
jQuery.post(ajaxurl, data, function (response) {
console.log(\'del response: \' + response);
})
}
function commentapprove(id) {
console.log(\'id: \' + id);
var data = {
\'action\': \'approvecomment\',
\'comment_id\': id
};
jQuery.post(ajaxurl, data, function (response) {
console.log(\'approve response: \' + response);
})
}
</script> <?php
$columns[\'comment_action\'] = __( \'No email\' );
return $columns;
}
add_filter( \'manage_edit-comments_columns\', \'addCommentColumns\', 10, 2);
function comment_action_column_buttons( $column, $comment_ID )
{
if ( \'comment_action\' == $column ) {
// document.location.reload(); // ignore this line please !
?>
<a style="cursor: pointer" onclick="commentapprove(<?php echo $comment_ID ?>);">Approve but don\'t email</a><br>
<a style="cursor: pointer" onclick="commentdel(<?php echo $comment_ID ?>); ">Trash but don\'t email</a>
<?php
}
}
add_filter( \'manage_comments_custom_column\', \'comment_action_column_buttons\', 10, 2 );
如果成功,我应该在JS控制台中看到“test”,但我看到的只是失败:
SO网友:Johansson
您可以将AJAX请求转换为REST端点,以消除这种常见的0响应。您只需为您的请求注册2条REST路由:
add_action( \'rest_api_init\', function () {
//Path to approve comment
register_rest_route( \'john_doe/v2\', \'/approve_comment/\', array(
\'methods\' => \'GET\',
\'callback\' => \'approve_my_comment\'
) );
// Path to delete comment
register_rest_route( \'john_doe/v2\', \'/remove_comment/\', array(
\'methods\' => \'GET\',
\'callback\' => \'remove_my_comment\'
) );
});
现在,在回调函数中:
function approve_my_comment(){
return \'Approved\';
}
function remove_my_comment(){
return \'removed\';
}
现在,您可以通过访问
example.com/wp-json/john_doe/v2/approve_comment
.
仅此而已。不再磨磨蹭蹭,没有回应。
请记住,REST回调函数必须返回一个值,而不是回显它。