I want to create a plugin that does some work in the background. I have a user interface in the admin backend where I have some buttons:

  • start fetching posts
  • stop fetching

When I press start fetching the plugin fetches some texts from an external API and creates a WordPress post for each text. But that are about 500.000 texts which have to be fetched. Since WordPress is based on PHP and I am not able to activate ZTS (thread-safety) for PHP on my webserver, each request for each text is made sequentially

  1. send request to API
  2. process the response
  3. create a WordPress post (and maybe category) for the text
  4. if there are texts left, goto 1.

So for about 500.000 texts this takes a lot of time. I wanted to know if you have an idea or a solution how to make the script faster or maybe how to use multi-threading, even if I don’t have the ability to activate ZTS.

1 Answer
1

You need to use WP Background Processing which fire off non-blocking asynchronous requests.

WooCommerce also use this for background processing. Check the file /includes/libraries/wp-background-process.php

Leave a Reply

Your email address will not be published. Required fields are marked *