Created
June 16, 2017 12:05
-
-
Save anthonysbrown/a0e025524314618e4ca3d1c386e57d22 to your computer and use it in GitHub Desktop.
WordPress Limit posts by ip address
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
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