1

I'm developing a comment plugin that sends the comment's content to a third party API and based on the response it either approves, trashes the comment or leaves it as pending.

The problem is that the response takes up to 30 seconds to come, so the user clicks "Post Comment" and sees the loading icon moving on his tab for up to 30 seconds, finally the page reloads and he either sees his approved comment posted, sees nothing has changed at all or sees the "awaiting moderation message" (approved, trashed, pending status respectively).

  1. Where should i hook the API request function so the the user always sees the "awaiting moderation" message without any delay and then the API request happens and the comment's status is set accordingly?
  2. Are there any hooks being activated during "dead time"? meaning the API requests can happen without the users experiencing delay and without requiring an admin to do anything to manually trigger them

I've tried hooking it on the wp_insert_comment action hook and got the results i mentioned above. Then i tried hooking it on the comment_post action hook, same outcome, only difference being that comment_post happens a bit later, after the comment has passed through the settings -> discussion filters the user has set.

Thanks!

4
  • Akismet must already do this? I'd see how they do it.
    – Rup
    Aug 12 at 16:38
  • I'd probably put the comment into a pending state and show the user that, then kick off the approval status in the background. Then when that completes update the final status in the database, and the user will see that next time they come back.
    – Rup
    Aug 12 at 16:39
  • @Rup Hey, could you clarify what you mean by "kick off the approval status in the background"? Is there another way to do that besides using wordpress cron?
    – PantelD
    Aug 16 at 12:08
  • 1
    Yes, something like you've done; if you've got lots more jobs than you've got traffic that won't scale well, but should be fine from what you've described. The other approaches I've seen are cron but setting up a background service to run the WordPress cron jobs rather than rely on the built-in cron, or Delicious Brains have a plugin that makes its own requests to trigger cron jobs. I'd stick with what you've got through.
    – Rup
    Aug 16 at 13:02
0

The desired results mentioned in the question were achieved by using the wp_schedule_event() function, now:

  1. The user always sees the "awaiting moderation" message without experiencing any delay.
  2. The API requests happen without the users experiencing delay, they happen asynchronously when anyone visits the page and the time interval given as argument to the wp_schedule_event() has passed.

Notice that the wp_schedule_event() line should only run once since the plugin gets activated, one way to do this is:

if ( ! wp_next_scheduled( 'bl_cron_hook' ) ) {
  wp_schedule_event( time(), 'five_seconds', 'bl_cron_hook' );
}

You can read more here.

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.