Create custom caching WordPress open

Create custom caching WordPress

function get_posts_ajax_handler() {

    $cache_file = '/path/to/cache/directory/cache_file.txt';
    if (file_exists($cache_file) && (time() - filemtime($cache_file)) < 3600) {
       
        $response = file_get_contents($cache_file);
        echo $response;
        wp_die();
    }

    
    $posts = // Our content

   
    $response = json_encode($posts);

   
    file_put_contents($cache_file, $response);

    echo $response;
    wp_die();
}
add_action('wp_ajax_get_posts', 'get_posts_ajax_handler');
add_action('wp_ajax_nopriv_get_posts', 'get_posts_ajax_handler');
0

More

Leave a Reply

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

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation