airesvsg/wp-rest-api-cache

启用 WordPress REST API 缓存,提高应用程序的速度。

安装: 391

依赖项: 0

建议者: 0

安全: 0

星标: 248

关注者: 10

分支: 37

公开问题: 10

类型:wordpress-plugin

dev-master 2019-02-28 13:02 UTC

This package is auto-updated.

Last update: 2024-09-12 06:09:12 UTC


README

启用 WordPress REST API 缓存,提高应用程序的速度

安装

  1. wp-rest-api-cache 文件夹复制到您的 wp-content/plugins 文件夹中
  2. 通过插件管理页面激活 WP REST API Cache 插件

过滤器

如何使用过滤器

  • 发送头部信息
add_filter( 'rest_cache_headers', function( $headers ) {
	$headers['Cache-Control'] = 'public, max-age=3600';
	
	return $headers;
} );
  • 更改缓存超时
add_filter( 'rest_cache_timeout', function() {
	// https://codex.wordpress.org/Transients_API#Using_Time_Constants
	return 15 * DAY_IN_SECONDS;
} );

或者

add_filter( 'rest_cache_get_options', function( $options ) {
	if ( ! isset( $options['timeout'] ) ) {
		$options['timeout'] = array();
	}

	// https://codex.wordpress.org/Transients_API#Using_Time_Constants
	$options['timeout']['length'] = 15;
	$options['timeout']['period'] = DAY_IN_SECONDS;
	
	return $options;
} );
  • 跳过缓存
add_filter( 'rest_cache_skip', function( $skip, $request_uri ) {
	if ( ! $skip && false !== stripos( $request_uri, 'wp-json/acf/v2' ) ) {
		return true;
	}

	return $skip;
}, 10, 2 );
  • 显示/隐藏管理链接

WP REST API Cache

  • 在保存帖子时清空缓存

您可以使用 WordPress 默认的过滤器 "save_post",如果您想在每个帖子、页面或自定义帖子类型的保存时清空缓存。

add_action( 'save_post', function( $post_id ) {
  if ( class_exists( 'WP_REST_Cache' ) ) {
    WP_REST_Cache::empty_cache();
  }
} );