Answer the question
In order to leave comments, you need to log in
How to execute a function asynchronously in WP (after sending the template to the user)?
Good time everyone.
Can you tell me how in WP it is possible to execute functions asynchronously without forcing the user to wait for its execution?
In 2 words. From the remote server I receive an array with ~50 objects (it changes periodically) that contain links to images. The images are very large, so when I first receive the array, I want to download images to the server in the background, reduce their size and insert their reduced copy into the template, for faster page loading for the end user.
Tried to do something like this
function cache_images() {
//скачивание, уменьшение размера изображений
}
add_action('cache_images', 'cache_images', 10, 1);
function render_template() {
// получение массива
// если (еще) нет сохраненных изображений
// вставляю оригинальные
// собственно тут и строится шаблон
wp_schedule_single_event(time(), 'cache_images', $listings);
}
Answer the question
In order to leave comments, you need to log in
By crown. Get it, put it in the cache.
Generally to receive on http and at once to give - not the best idea. Do it through transit caches https://wp-kama.ru/id_6462/wordpress-http-api.html
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question