The desired results mentioned in the question were achieved by using the wp_schedule_event() function, now:
- The user always sees the “awaiting moderation” message without experiencing any delay.
- 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.