Skip to content

Instantly share code, notes, and snippets.

@dryan1144
Last active December 24, 2019 08:20
Show Gist options
  • Save dryan1144/480b82e6cdc70977d77b0dca20ce7690 to your computer and use it in GitHub Desktop.
Save dryan1144/480b82e6cdc70977d77b0dca20ce7690 to your computer and use it in GitHub Desktop.
Sets a transient for elements in a WP REST API call
<?php
function yournamespace_rest_transient_example() {
//check for transient first
$data = get_transient( 'yournamespace_data_example' );
if ( !empty( $data ) ) {
$data = $terms_transient;
//generate output and then save to transient
} else {
//do some stuff here and set result to $data
$data = custom_remote_post_call();
//cache for 2 days
set_transient( 'yournamespace_data_example', $data, DAY_IN_SECONDS * 2 );
}
$result = new WP_REST_Response( $data, 200 );
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment