I have some functions that run fairly long to complete. These functions are called via hooks registration (i.e. add_action() and add_filter()). Would it impact the end-users when my tasks runs for a lengthly period or hooks run asynchronously?


2 Answers 2


Actions and filters are resolved in a linear fashion when processing the request. They do not run asynchronously. If you have some heavy processing that runs during every request (i.e. you use action and filter hooks that run during every page load for every user, like template_redirect, init or whatever usual hooks you might be using), then it will impact the loading time of the page and probably consume lots of your server's CPU time.

If you do some heavy data processing that doesn't need to run for every page load (that is not calculated for every user and doesn't have to be accessible for users during casual browsing) you might consider using WP Cron or setup a cron job on your server.

Otherwise, you might use AJAX to load the needed data asynchronously, that is display the page without the special processed data first, then send an AJAX request to the server which would fetch the data. This way the initial page load is fast and the user sees some kind of loading animation and knows that your website is working on fetching the data.

  • Thanks for suggesting WP Cron; hopefully it's an easy task to set it up. Upon new posts being created, i have a method that goes through a list of subscribers to email and push notify them. These tasks could be lengthy and that is why i ask this to be sure.
    – Loc Pham
    Nov 17, 2015 at 18:04
  • @LocPham WP Cron would be ideal in this situation. What you can do is either set up a cron job that runs hourly and checks for new posts added. You could also schedule only a single cron job on post publish and clear the scheduled event once the processing is done (e-mails and notifications sent). The latter would be more efficient as the scheduled event will only run when needed. If you ever want to show notifications about new content to users on the website itself - the AJAX method would be the best. :) Nov 17, 2015 at 19:10
  • One more thing - if your mailing list is huge you can run out of memory or processing time for the script. To prevent this you would setup the cron job to only send a portion of notifications at a time and do that until all notifications are sent. Nov 17, 2015 at 19:12

Whatever the action/filter you hooked your code into they (the callbacks) DO NOT run asynchronously.

PHP is not Javascript/Java/C# or any other programming language & compiler/interpreter that permits async functions, user-defined threads and etc.. Why would you think this? PHP is a text processor that allows the creation of dynamic website. Upon a set of parameters is returns a certain HTML result. It has to be fast as the Flash and it cannot wait for async functions to end before sending its result.

Your site lags because your functions do some heavy duty stuff there.


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.