I have built a plugin that fires an Ajax call to connect to a third-party API. That API returns some Json, which I then save as a custom post with wp_insert_post
.
Now, usually, this works. But, now and again, the save fails, with a “MySQL server has gone away” error. I’m assuming this is because the API took too long to respond (maybe 30 secs), and so the connection closed.
Does this sound like the correct conclusion? And if so, how can I handle this from within the plugin?
EDIT: 20 secs doesn’t seem long enough for a timeout, so I’m baffled by this one…
You could try what
wp-cron.php
does and set in your AJAX handler:But this might cause other problems, for example when the API takes an hour to respond. 🙂
Try to log all API response time for while to see if that is really the problem.
At risk of giving the obvious answer you probably already tried… assuming everything else is good to go, WordPress does cap execution time at 30 seconds (I think). 20 seconds could seem like 30 pretty easily yea?
The codex recommends adding
php_value max_execution_time 60
to your .htaccess file andmax_execution_time = 60 ;
and you could also try adding set_time_limit(x) in your wp-config or on the script that fires the ajax in your plugin.