Skip to content

Instantly share code, notes, and snippets.

@sepehr125
Last active August 29, 2015 14:14
Show Gist options
  • Save sepehr125/3535aeb887c30826f9e2 to your computer and use it in GitHub Desktop.
Save sepehr125/3535aeb887c30826f9e2 to your computer and use it in GitHub Desktop.
Make a WordPress site inaccessible, except via a secret parameter in URL, which then grants access to site for number of days
/*
*
* Make a WordPress site inaccessible except via a secret
* parameter in the URL. Once that URL is visited,
* grant the visitor access for a number of days via a cookie.
*
* Gateway URL is of the form: example.com/?{{ secret }}
*/
add_action('init', 'secret_gateway_only_access');
function secret_gateway_only_access() {
$secret = "long_alphanumeric_string_here_123_xyz";
$num_days_of_access = 3;
$cookie_name = "unique_prefix_access_key";
if ( isset($_GET[$secret]) ) {
setcookie($cookie_name, $secret, time()+3600*24*$num_days_of_access);
wp_redirect(site_url());
}
if ( $_COOKIE[$cookie_name] !== $secret ) {
die();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment