Created
February 3, 2014 04:47
-
-
Save atwellpub/8778976 to your computer and use it in GitHub Desktop.
Secure WordPress JSON API with API Key
This file contains 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 | |
/* place in theme's functions.php file */ | |
/* disable json api if api key not correct */ | |
add_action('init','json_api_apikey_check' , 1 ); | |
function json_api_apikey_check() | |
{ | |
$api_key = 'hello'; | |
$base = get_option('json_api_base', 'api'); | |
$this_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; | |
if ( ( isset($_REQUEST['json'] ) || strstr($this_url , $base.'/') ) && ( !isset($_REQUEST['apikey']) || $_REQUEST['apikey'] != 'hello' ) ) | |
{ | |
print "[error:99] Permission denied!"; | |
exit; | |
} | |
} |
This is what I am exactly looking for, but how does the $api_key gets passed from the caller? Does that need to be query string?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I suggest that line 13 is replaced with: