Skip to content

Instantly share code, notes, and snippets.

@Mte90
Created November 26, 2019 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mte90/a54c38d188422e35e548b026c783aaeb to your computer and use it in GitHub Desktop.
Save Mte90/a54c38d188422e35e548b026c783aaeb to your computer and use it in GitHub Desktop.
Block WordPRess rest api except logged user or if there is a parameter in the url
<?php
add_filter('rest_authentication_errors', function ($result) {
if (!empty($result)) {
return $result;
}
$access = is_user_logged_in();
if ( !$access ) {
if ( !isset($_GET['iwantthem']) || isset($_GET['iwantthem']) && $_GET['iwantthem'] !== 'yes' ) {
$access = false;
} else {
$access = true;
}
}
if ( !$access ) {
return new WP_Error('rest_not_logged_in', 'You are not currently logged in or you don\'t have access.', array('status' => 401));
}
return $result;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment