On the edit-comments.php page, I'm hooking into the comment_row_actions filter to add another link at the end. I'm copying the "Approve" link, adding another query key and value and modifying the anchor text. I want to add some comment meta based on the value of the new query key.
Approved link:
/wp-admin/comment.php?c=9999&action=approvecomment&_wpnonce=8526c66
Approved w/ meta link
/wp-admin/comment.php?c=9999&action=approvecomment&metalink=1&_wpnonce=8526c66
The URL prints out just fine so the comment_row_actions filter is performing as expected.
Since that URL calls wp_transition_comment_status()
, I'm hooking into that to check for the value of $_GET['metalink']
and setting some comment metadata off of that in the hooked function.
The edit-comments.php script loads JS that takes some traditional anchor tag links and makes them AJAX calls for approving, trashing, and unapproving comments. I would expect clicking my new link to mimic the AJAX call for the Approved link but apparently the presence of another query variable is a problem. The hooked function is running but the $_GET['metalink']
variable is not available to the function and the AJAX message/behavior is not the same as the Approve link behavior.
Best I can tell is that WordPress may be dropping the extra query variables for security reasons which is understandable.
Any suggestions for passing a variable in the url query string that will work in the AJAX environment?
On the edit-comments.php page, I'm hooking into the comment_row_actions filter to add another link at the end. I'm copying the "Approve" link, adding another query key and value and modifying the anchor text. I want to add some comment meta based on the value of the new query key.
Approved link:
/wp-admin/comment.php?c=9999&action=approvecomment&_wpnonce=8526c66
Approved w/ meta link
/wp-admin/comment.php?c=9999&action=approvecomment&metalink=1&_wpnonce=8526c66
The URL prints out just fine so the comment_row_actions filter is performing as expected.
Since that URL calls wp_transition_comment_status()
, I'm hooking into that to check for the value of $_GET['metalink']
and setting some comment metadata off of that in the hooked function.
The edit-comments.php script loads JS that takes some traditional anchor tag links and makes them AJAX calls for approving, trashing, and unapproving comments. I would expect clicking my new link to mimic the AJAX call for the Approved link but apparently the presence of another query variable is a problem. The hooked function is running but the $_GET['metalink']
variable is not available to the function and the AJAX message/behavior is not the same as the Approve link behavior.
Best I can tell is that WordPress may be dropping the extra query variables for security reasons which is understandable.
Any suggestions for passing a variable in the url query string that will work in the AJAX environment?
Share Improve this question asked May 26, 2017 at 16:56 jdpjdp 3532 silver badges10 bronze badges 2 |1 Answer
Reset to default 2I resolved this issue by using the wp-ajax environment. Essentially, I created the callback function for the comment_row_actions filter which created the link pointing to admin-ajax.php in the admin directory with a query string. Then I created the add_action to run in the wp_ajax environment with a redirect back to the referring page. I couldn't come up with a solution that routed through comment.php.
Code below:
function my_filter_comment_row_actions($actions, $comment) {
if ($comment->comment_approved == '0') {
//build link
$nonce = wp_create_nonce('mynonce');
$screen = get_current_screen();
$args = array(
'c' => $comment->comment_ID,
'action' => 'my_ajax_action',
'another_query' => '1',
'_wpnonce' => $nonce,
'refer' => $screen->parent_file
);
$link = esc_url(add_query_arg($args, admin_url('admin-ajax.php')));
$actions['the_new_link'] = sprintf('<a href="%s" style="color:green">The New Link Text</a>', $link);
}
return $actions;
}
add_filter('comment_row_actions', 'my_filter_comment_row_actions', 10, 2);
So there's the filter with callback to create the link. Now to the "AJAX" which really isn't AJAX.
function my_approve_comment_with_tracking() {
if (wp_verify_nonce($_REQUEST['_wpnonce'], 'mynonce')) {
//do stuff
wp_redirect(admin_url($_REQUEST['refer']));
} else wp_redirect(home_url());
exit;
}
add_action('wp_ajax_my_ajax_action', 'my_approve_comment_with_tracking');
Hope this helps someone.
document.ready
event to capture the query var and store it in a global JS var for the click handlers to access when needed. – scott Commented May 26, 2017 at 22:29