1

I have the following code in the functions.php file and I am trying to modify it so it only loads the google sheets file once a day, stores that data, and for the rest of the day the shortcode reads the data from the loaded file instead of accessing the google sheet at each page load.

function sheet_value_shortcode($atts) {
    $API = 'XXX';
    $google_spreadsheet_ID = 'XXX';
    $api_key = esc_attr( $API);

    $location = $atts['location'];

    $get_cell = new WP_Http();
    $cell_url = "https://sheets.googleapis.com/v4/spreadsheets/$google_spreadsheet_ID/values/$location?&key=$api_key";  
    $cell_response = $get_cell -> get( $cell_url);
    $json_body = json_decode($cell_response['body'],true);  
    $cell_value = $json_body['values'][0][0];
    return $cell_value;
}
add_shortcode('get_sheet_value', 'sheet_value_shortcode');

Any suggestions? Thanks a lot!

  • 2
    I would suggest splitting the code. First create a script that fetches the google sheet once a day using cron job and store it in a folder. Second load the file via that folder. – Muhammad Tashfeen Nov 12 '20 at 20:12
  • What have you tried so far? Where are you stuck? It should not be too hard to use a cronjob for this – Nico Haase Jan 15 '21 at 13:07

0 Answers0