Skip to content

Instantly share code, notes, and snippets.

@juanmaguitar
Last active October 6, 2019 09:34
Show Gist options
  • Save juanmaguitar/0b73b7970fba1e8b7a19fa9995d9ebaa to your computer and use it in GitHub Desktop.
Save juanmaguitar/0b73b7970fba1e8b7a19fa9995d9ebaa to your computer and use it in GitHub Desktop.
Disable endpoints Wordpress API
/* ---- endpoints API ---- */
function disableAllEndpoints( $endpoints ){
foreach ($endpoints as $clave => $valor) {
unset( $endpoints[$clave] );
}
return $endpoints;
}
function disableOnlyEndpoints( $endpoints ){
$ep_only = array(
"/wp/v2/posts",
"/wp/v2/categories"
);
foreach ($endpoints as $clave => $valor) {
foreach ($ep_only as $only) {
if ( substr($clave, 0, strlen($only)) === $only ) {
unset( $endpoints[$clave] );
}
}
}
return $endpoints;
}
function disableAllEndpointsButExceptions( $endpoints ){
$ep_exceptions = array(
"/wp/v2/posts",
"/wp/v2/categories",
);
foreach ($endpoints as $clave => $valor) {
$to_unset = true;
foreach ( $ep_exceptions as $exception) {
if ( $clave === $exception) {
$to_unset = false;
}
}
if ($to_unset) {
unset( $endpoints[$clave] );
}
}
return $endpoints;
}
//add_filter( 'rest_endpoints', 'disableAllEndpoints' );
//add_filter( 'rest_endpoints', 'disableOnlyEndpoints' );
//add_filter( 'rest_endpoints', 'disableAllEndpointsButExceptions' );
// Disable ALL endpoints (from here we can create our custom ones)
remove_action( 'rest_api_init', 'create_initial_rest_routes', 0 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment