Skip to content

Instantly share code, notes, and snippets.

@anthonysbrown
Created June 16, 2017 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonysbrown/a0e025524314618e4ca3d1c386e57d22 to your computer and use it in GitHub Desktop.
Save anthonysbrown/a0e025524314618e4ca3d1c386e57d22 to your computer and use it in GitHub Desktop.
WordPress Limit posts by ip address
add_action('init','wp_limit_post_by_ip');
function wp_limit_post_by_ip(){
global $post;
#posts this is required, comma seperate
$post_ids = array(12);
#array of ips comma seperate
$allowed_ips = array('127.0.0.1');
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
if(in_array($post->ID,$post_ids)){
if(!in_array($ip,$allowed_ips)){
wp_redirect(home_url( ));
exit;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment