Skip to content

Instantly share code, notes, and snippets.

@mayeenulislam
Last active June 22, 2018 16:27
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 mayeenulislam/81fe6a2f2b37bd331e918e1761a744d4 to your computer and use it in GitHub Desktop.
Save mayeenulislam/81fe6a2f2b37bd331e918e1761a744d4 to your computer and use it in GitHub Desktop.
WP Restrict Unwanted Users - restrict unwanted users from visiting the site (Link: https://wordpress.stackexchange.com/a/306082/22728)
<?php
/**
* Plugin Name: WPSE Restrict Unwanted Users
* Plugin URI: https://wordpress.stackexchange.com/a/306082/22728
* Description: Restrict unwanted users from visiting the site
* Author: Mayeenul Islam
* Version: 1.0.0
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
/**
* Get the user's IP address
*
* For the prohibition of multiple vote count on Star Rating, we are grabbing the
* user's IP address and storing it to database for the time we want to restrict
* the user into voting again.
*
* @author Barış Ünver
* @link http://code.tutsplus.com/articles/creating-a-simple-contact-form-for-simple-needs--wp-27893
*
* @return string IP address, formatted.
* ------------------------------------------------------------------------------
*/
function wpse_306059_22728_get_the_ip() {
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
return $_SERVER["HTTP_X_FORWARDED_FOR"];
}
elseif (isset($_SERVER["HTTP_CLIENT_IP"])) {
return $_SERVER["HTTP_CLIENT_IP"];
}
else {
return $_SERVER["REMOTE_ADDR"];
}
}
/**
* Restrict the user and display a message.
*
* @see wpse_306059_22728_get_the_ip()
* @see wp_die()
*
* @return void
* ------------------------------------------------------------------------------
*/
function wpse_306059_22728_restrict_user() {
// Get the list of blocked IP address in comma separated string.
$black_list = '0.0.0.0, 1.1.1.1, ::1'; // use get_option('your_spam_user_block_list')
// make it an array
$block_array = explode(',', $black_list);
$block_array = array_map('trim', $block_array); // remove the spaces
// get current user's IP address
$current_user_ip = wpse_306059_22728_get_the_ip();
// Restrict the user
if( in_array($current_user_ip, $block_array) ) {
$message = '<h3>'. __('Access Restricted', 'wpse-restrict-unwanted-users') .'</h3>';
$message .= '<p>'. __( 'It seems the IP address you are using is spamming us. We are sorry for your inconvenience, but your access to the site is restricted.', 'wpse-restrict-unwanted-users' ) .'</p>';
$message .= '<p>'. sprintf( __( 'If the message seems wrong to you, please contact us by emailing at: <strong>%s</strong>', 'wpse-restrict-unwanted-users' ), antispambot( 'yourname@example.com' ) ) .'</p>';
wp_die(
$message, // message
__( 'Access Restricted', 'wpse-restrict-unwanted-users' ) // page title
);
}
}
add_action( 'wp', 'wpse_306059_22728_restrict_user' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment