Skip to content

Instantly share code, notes, and snippets.

@runezero
Last active June 13, 2022 13:48
Show Gist options
  • Select an option

  • Save runezero/da468129856f5d6ddc027a8a812a8a5c to your computer and use it in GitHub Desktop.

Select an option

Save runezero/da468129856f5d6ddc027a8a812a8a5c to your computer and use it in GitHub Desktop.
[Get user IP] Get the IP-address from the user and check if it exists in the site owner list #wordpress #enfold
//function to retrieve user IP address
function wpdev_get_user_ip(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
//ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
//ip pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
//set siteowner cookie if IP is in array
add_action('init', function(){
//array with IP addresses of site owners
$site_owner_ip = array("8.8.8.8");
//check if user ip is in array of owner ip's
if (in_array(wpdev_get_user_ip(), $site_owner_ip)) {
//if cookie is not set, set cookie
if (!isset($_COOKIE["site_owner"])){
setcookie("site_owner", 'true', time()+3600);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment