Skip to content

Commit 402c5a0

Browse files
committed
feat(wordpress): support API caching
1 parent c8aa6d2 commit 402c5a0

File tree

1 file changed

+19
-1
lines changed
  • packages/smooth-backend-wordpress/src/wordpress/plugin

1 file changed

+19
-1
lines changed

packages/smooth-backend-wordpress/src/wordpress/plugin/index.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,22 @@ function get_preview_for_url($data)
206206
$fields = get_fields_recursive($post);
207207
$data['acf'] = $fields->acf;
208208
return rest_ensure_response( $data );
209-
}, 10, 2 );
209+
}, 10, 2 );
210+
211+
// API Caching
212+
213+
// Skip cache for preview
214+
add_filter( 'rest_cache_skip', function($skip, $request_uri) {
215+
if (!$skip && false !== stripos( $request_uri, 'wp-json/presspack/v1/preview')) {
216+
return true;
217+
}
218+
219+
return $skip;
220+
}, 10, 2 );
221+
222+
// Clear cache when a post (all types) is saved
223+
add_action('save_post', function($post_id) {
224+
if (class_exists('WP_REST_Cache')) {
225+
WP_REST_Cache::empty_cache();
226+
}
227+
});

0 commit comments

Comments
 (0)