2

In the admin panel's "Edit Comment" page, for some reason after I click "Update" the Edit page goes into infinite loop/loading (the spinner icon in the Google Chrome tab never stops spinning). My computer goes into "deep think" for 10-20 minutes, even after I close WordPress, the web browser, and my IDE (I'm working locally on Microsoft WebMatrix 3).

Calling wp_update_comment() seems to be the problem, because when I remove it everything works fine. When I check the database I can see that the 'comment_type' column does get updated though, but my computer still goes into a deep think consuming resources and the Edit page goes into infinite loading.

function update_product_comment($comment_ID) {
    $commentarr = [];
    $commentarr['comment_ID'] = $comment_ID;
    $commentarr['comment_type'] = 'test';

    //wp_die(var_dump($commentarr['comment_type'])); <-- COMMENT_TYPE = 'TEST', SO FAR SO GOOD!
    wp_update_comment($commentarr); // <--- THE CULPRIT
}
add_action('edit_comment', 'update_product_comment');

I don't know what could be causing wp_update_comment() to do this, I have no plugins installed and a basically fresh WordPress install, default wordpress theme, and no custom code. But the same problem applies no matter what theme I use. Maybe it's WebMatrix I don't know, can someone test the code and tell me if it works?

2 Answers 2

2

wp_update_comment triggers the edit_comment action (source), you're creating an infinite loop.

3
  • THANKS! It's been driving me nuts, no wonder! Hmm... I'm making a comment_type field in the comment edit page. I guess I'll need a new idea to edit the comment_type field... Nov 1, 2014 at 2:00
  • 1
    A static counter to terminate the loop after 1 go does the trick. Nov 1, 2014 at 5:27
  • Instead of implementing a counter, just remove the action at the beginning of the callback to avoid the infinite loop, see my complementary answer for details. @Blastercloud Nov 2, 2014 at 21:34
0

Just remove the action hook at the beginning of your callback function to avoid the infinite loop - for this you can add:

// avoid infinite loop
remove_action( 'edit_comment', 'update_product_comment' );

Could be alternatively done like this:

// avoid infinite loop
remove_action( 'edit_comment', __FUNCTION__ );

Another variation would be:

// avoid infinite loop
remove_action( current_action(), __FUNCTION__ );

Just to note that too, only one of above variants is needed.

1
  • My pleasure. The approach works on any hook where you call the the function containing the hook itself, I guess most prominently you'll see this on pre_get_posts callbacks where a WP_Query is done to get post IDs. @Blastercloud Nov 2, 2014 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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