Skip to content

Instantly share code, notes, and snippets.

@betweenbrain
Created June 18, 2018 18:11
Show Gist options
  • Save betweenbrain/65a611b0c88d74a9640420a6a598e310 to your computer and use it in GitHub Desktop.
Save betweenbrain/65a611b0c88d74a9640420a6a598e310 to your computer and use it in GitHub Desktop.
WordPress plugin to capture REST API cookie header
<?php
/**
 * Plugin Name: Cookie Check
 * Plugin URI: http://guggenheim.org/
 * Author: Matt Thomas
 * Version: 0.0.1
 * Author URI: http://guggenheim.org/
 */
 
class Cookie_Check {

	/**
	 * Constructor.
	 */
	public function __construct() {
		if ( ! is_admin() && ! $this->is_login_page() && $this->is_rest_request() ) {
			add_action(
				'init',
				function () {
					// Do something with $_COOKIE
				}
			);
		}
	}

	/**
	 * Check if the current page is a login form.
	 *
	 * @return boolean
	 */
	public function is_login_page() {
		return in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) );
	}

	/**
	 * Check if the current request is a REST request.
	 *
	 * @return boolean
	 */
	public function is_rest_request() {
		return strpos( $GLOBALS['_SERVER']['REQUEST_URI'], 'wp-json' );
	}
}

new Cookie_Check();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment