Created
July 5, 2019 07:35
-
-
Save mightymt/461c804837aed7d3f93278d9842a9a47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: MT REST Cache Test | |
Plugin URI: http://localhost | |
Description: MT REST Cache Test | |
Version: 1.0 | |
Author: mighty_mt | |
Author URI: http://localhost | |
Licence: GPLv3 | |
*/ | |
/** | |
* This is our callback function that embeds our phrase in a WP_REST_Response | |
*/ | |
function mtrct_get_endpoint_phrase() { | |
$response = array( | |
"type" => "FeatureCollection", | |
"features" => array( | |
array( | |
"type" => "Feature", | |
"geometry" => array( | |
"type" => "Point", | |
"coordinates" => array( | |
"16.37313723564148", | |
"48.20847346029796" | |
), | |
), | |
"properties" => array( | |
"name" => "Stephansdom", | |
), | |
), | |
), | |
); | |
return new WP_REST_Response( $response, 200 ); | |
} | |
/** | |
* This function is where we register our routes for our example endpoint. | |
*/ | |
function mtrct_register_example_routes() { | |
// register_rest_route() handles more arguments but we are going to stick to the basics for now. | |
register_rest_route( 'mtrct/v1', '/test', array( | |
// By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended. | |
'methods' => WP_REST_Server::READABLE, | |
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class. | |
'callback' => 'mtrct_get_endpoint_phrase', | |
) ); | |
} | |
add_action( 'rest_api_init', 'mtrct_register_example_routes' ); | |
/** | |
* Register the /wp-json/acf/v3/posts endpoint so it will be cached. | |
*/ | |
function mtrct_add_acf_posts_endpoint( $allowed_endpoints ) { | |
if ( ! isset( $allowed_endpoints[ 'mtrct/v1' ] ) || ! in_array( 'test', $allowed_endpoints[ 'mtrct/v1' ] ) ) { | |
$allowed_endpoints[ 'mtrct/v1' ][] = 'test'; | |
} | |
return $allowed_endpoints; | |
} | |
add_filter( 'wp_rest_cache/allowed_endpoints', 'mtrct_add_acf_posts_endpoint', 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment