0

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?

  • I'd debug the code by using console.log to see if the click action can access the variable before the AJAX call is fired. If it doesn't, I'd consider using the 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 May 26 '17 at 22:29
  • In /wp-admin/js/edit-comments.js, the desired callback behavior seems to be keyed to the anchor tag's parent span class. In this case using my filter, the span class can not be approved, otherwise I will overwrite the core's "approved" span class. So I'd have to write some JS to add that the "approved" class to the parent span on the click event. That doesn't do a thing to passing the query variable to /wp-admin/comment.php though. I can't pinpoint where the JS is modifying the anchor tag click action in the core. edit-comments.js would be the likely place but I don't see it there. – jdp May 27 '17 at 16:52
0

I 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.