Created
November 26, 2019 10:55
-
-
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
This file contains hidden or 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 | |
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