WordPress管理员AJAX Trash_Comment

时间:2019-06-17 作者:n.courouppe

当有人从wp管理员删除评论时,我试图添加一个trash\\u原因。我设法添加了一个带有dropbown的thickbox,列出了删除原因,并在垃圾链接的query\\u字符串中传递/更新了selected选项的值,但我在add\\u操作中无法获得它

这是我的代码:

add_filter(\'comment_row_actions\', \'filter_comments_actions\', 1, 2);
function filter_comments_actions($actions, $comment)
{
global $wpdb;


$del_nonce = esc_html(\'_wpnonce=\' . wp_create_nonce("delete-comment_$comment->comment_ID"));
$trash_url = $trash_url = esc_url("comment.php?action=trashcomment&p=$comment->comment_post_ID&c=$comment->comment_ID&$del_nonce&reason=1");
add_thickbox();
$reasons = $wpdb->get_results(\'SELECT * FROM wp_jb_rejected_review_reasons\');
$actions[\'trash\'] = "
    <a href=\'#TB_inline?&width=400&height=200&inlineId=comment-actions-id\' class=\'thickbox\'>Trash</a> 
    <div id=\'comment-actions-id\' style=\'display: none;\'>
        <h3>
            Reason for deletion
        </h3>
        <form action=\'\'>
            <select onchange=\'update_moderate_reason_id($comment->comment_ID)\' name=\'rejected_reason\' id=\'rejected_reason_$comment->comment_ID\'>
        ";
foreach ($reasons as $reason) {
    $actions[\'trash\'] .= "<option value=\'$reason->id\'>$reason->reason</option>";
}
$actions[\'trash\'] .= "
            </select>
            <br/>";
$actions[\'trash\'] .= "<a onclick=\'log_moderation_href(this)\' href=\'$trash_url\' data-wp-lists=\'delete:the-comment-list:comment-$comment->comment_ID::trash=1\' class=\'delete vim-d vim-destructive confirm-deletion-comment-$comment->comment_ID\' aria-label=\'" . esc_attr__(\'Move this comment to the Trash\') . "\'>" . _x(\'Trash\', \'verb\') . \'</a>\';
$actions[\'trash\'] .=
    "
        </form>  
    </div>
    ";
unset($actions[\'edit\']);
return $actions; 
}


add_action(\'trash_comment\', \'log_trash_comment\', 1);

function log_trash_comment($comment_id)
{
    $reason = $_REQUEST[\'reason\'];
    global $wpdb;
    $wpdb->query("INSERT INTO wp_rejected_reviews (moderator_id, 
comment_id, reason_id) VALUES (".get_current_user_id().", $comment_id, 
$reason)");
}
和我的javascript:

function update_moderate_reason_id(comment_id) {

var reason_id = jQuery("select[name=\'rejected_reason\']#rejected_reason_"+comment_id).val();
console.log(reason_id);
var confirm_link = jQuery("a.confirm-deletion-comment-"+comment_id);
var _href = confirm_link.attr(\'href\');
if(_href.indexOf(\'&reason=\') != -1){
    _href = _href.substr(0, _href.length-9);
}
_href += "&reason="+reason_id;
confirm_link.attr(\'href\', _href); }

function log_moderation_href(link){
    console.log(link);
}
但是,当我在我的log\\u trash\\u comment函数中执行var\\u dump()$\\u请求时,不会显示原因

提前感谢您的宝贵帮助,

1 个回复
最合适的回答,由SO网友:MikeNGarrett 整理而成

向url添加参数的方式不起作用。ajax请求旨在防止这样的参数插入。

我建议您启动另一个ajax请求来处理这个过程,而不是试图以这种方式连接到现有的过程中。

例如,您可以在注释被丢弃后启动模式窗口,并单独控制提交和日志记录过程。