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 | Show 2 more comments1 Answer
Reset to default 0The 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.
add_action('trash_comment', 'log_trash_comment', 10, 1);
– majick Commented Jun 17, 2019 at 16:27jb_trash_comment()
? That's the only piece I'm missing in order to debug this. – MikeNGarrett Commented Jun 17, 2019 at 19:23