Last active
May 27, 2021 18:29
-
-
Save hegemanjr/78b6b9a27f30eac062a8be5a1151a4a6 to your computer and use it in GitHub Desktop.
Block REST API for non-logged in users
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 | |
/** | |
* Plugin Name: 608 Software - Block REST API | |
* Plugin URI: https://608.software | |
* Description: Block REST API for users that are not logged in | |
* Author: 608 Software | |
* Version: 1.0.2 | |
* Author URI: https://608.software | |
* Text Domain: soes-block-rest-api | |
* Domain Path: /languages | |
* License: GPL-2.0+ | |
*/ | |
class SOES_Block_REST_API { | |
function __construct() { | |
add_action('init', array($this, 'action_init')); | |
} | |
public function action_init() { | |
add_action( 'pre_get_posts', array($this, 'action_pre_get_posts' )); | |
add_filter( 'rest_authentication_errors', array($this, 'filter_rest_authentication_errors' )); | |
} | |
function action_pre_get_posts( $query ) { | |
// Do stuff here | |
} | |
function filter_rest_authentication_errors( $result ) { | |
if ( ! empty( $result ) ) { | |
return $result; | |
} | |
if ( ! is_user_logged_in() ) { | |
return new WP_Error( 'rest_not_logged_in', 'You are not currently logged in.', array( 'status' => 401 ) ); | |
} | |
return $result; | |
} | |
} | |
new SOES_Block_REST_API(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment