最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wordpress admin ajax trash_comment

programmeradmin0浏览0评论

I'm trying to add a trash_reason when someone delete a comment from the wp admin. I managed to add a thickbox with a dropbown listing deletion reasons and passing / updating the value of selected option in the query_string of the trashing link, but I can't get it in my add_action

Here is my code :

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)");
}

And my 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);
}

But when i var_dump() $_REQUEST in my log_trash_comment function, the reason does not appear

Thanks in advance for your precious help,

I'm trying to add a trash_reason when someone delete a comment from the wp admin. I managed to add a thickbox with a dropbown listing deletion reasons and passing / updating the value of selected option in the query_string of the trashing link, but I can't get it in my add_action

Here is my code :

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)");
}

And my 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);
}

But when i var_dump() $_REQUEST in my log_trash_comment function, the reason does not appear

Thanks in advance for your precious help,

Share Improve this question edited Jun 18, 2019 at 7:01 n.courouppe asked Jun 17, 2019 at 15:27 n.courouppen.courouppe 34 bronze badges 7
  • it does look alright, maybe console.log the final href also – majick Commented Jun 17, 2019 at 15:56
  • Hi, I just did that and the href is alright : "comment.php?action=trashcomment&p=86035&c=4&_wpnonce=0fb8114f8b&reason=6" – n.courouppe Commented Jun 17, 2019 at 16:10
  • longshot but maybe you need add_action('trash_comment', 'log_trash_comment', 10, 1); – majick Commented Jun 17, 2019 at 16:27
  • Hey, just tried it but no more success. What is really weird is that when I debug my code at the moment it enters my custom function, there is not $_GET, only $_POST with the ajax parameters for Wordpress. – n.courouppe Commented Jun 17, 2019 at 16:36
  • Can you post your javascript for jb_trash_comment()? That's the only piece I'm missing in order to debug this. – MikeNGarrett Commented Jun 17, 2019 at 19:23
 |  Show 2 more comments

1 Answer 1

Reset to default 0

The way you're adding parameters to the url isn't working. The ajax request is designed to prevent parameter insertion like this.

I recommend you kick off another ajax request to handle this process rather than trying to hook into the existing process in this way.

For example, you can kick off your modal window after a comment is trashed and control the submission and logging process separately.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论