Skip to content

Instantly share code, notes, and snippets.

@drewbaker
Last active June 19, 2017 09:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drewbaker/61e7ed9d905727bb570999e9e1a68f1f to your computer and use it in GitHub Desktop.
Save drewbaker/61e7ed9d905727bb570999e9e1a68f1f to your computer and use it in GitHub Desktop.
GeoIP functions for WordPress. Requires some external GeoIP files and .dat file from MaxMind.
/*
* Region Select Custom Post Type
*/
function cutandrun2016_post_type() {
// US editors
$us_post_labels = array(
'name' => _x('US Region', ''),
'singular_name' => _x('US Region', ''),
'add_new' => _x('Add New', 'editor'),
'add_new_item' => __('Add New'),
'edit_item' => __('Edit'),
'new_item' => __('New'),
'all_items' => __('All'),
'view_item' => __('View'),
'search_items' => __('Search'),
'not_found' => __('Not found'),
'not_found_in_trash' => __('Not found in the Trash'),
'parent_item_colon' => '',
'menu_name' => 'US Region'
);
$us_post_options = array(
'labels' => $us_post_labels,
'description' => '',
'public' => true,
'menu_position' => 5,
'supports' => array('title', 'editor', 'thumbnail', 'page-attributes'),
'hierarchical' => true,
'capability_type' => 'page',
'show_in_menu' => true,
'show_in_nav_menus' => true,
'menu_icon' => 'dashicons-admin-site'
);
register_post_type('us', $us_post_options);
// UK editors
$uk_post_labels = array(
'name' => _x('UK Region', ''),
'singular_name' => _x('UK Region', ''),
'add_new' => _x('Add New', 'editor'),
'add_new_item' => __('Add New'),
'edit_item' => __('Edit'),
'new_item' => __('New'),
'all_items' => __('All'),
'view_item' => __('View'),
'search_items' => __('Search'),
'not_found' => __('Not found'),
'not_found_in_trash' => __('Not found in the Trash'),
'parent_item_colon' => '',
'menu_name' => 'UK Region',
);
$uk_post_options = array(
'labels' => $uk_post_labels,
'description' => '',
'public' => true,
'menu_position' => 5,
'supports' => array('title', 'editor', 'thumbnail', 'page-attributes'),
'hierarchical' => true,
'capability_type' => 'page',
'show_in_menu' => true,
'show_in_nav_menus' => true,
'menu_icon' => 'dashicons-admin-site'
);
register_post_type('uk', $uk_post_options);
}
add_action('init', 'cutandrun2016_post_type');
/*
* GeoIP Detection - Guess what contry the user is in
* https://github.com/maxmind/geoip-api-php
*/
function get_geoip_region() {
// This is the IP database, get country by IP
include_once('GeoIP/geoip.inc');
$ip = $_SERVER['REMOTE_ADDR'];
$db = geoip_open(get_template_directory() . '/GeoIP/GeoIP-106_20150210.dat', GEOIP_STANDARD);
$country_code = strtolower( geoip_country_code_by_addr($db, $ip) );
geoip_close($db);
// Include country arrays
include('GeoIP/country-codes.php');
// Uppercase code
$country_code = strtoupper($country_code);
switch (true){
case in_array( $country_code, $europe) :
// User is in Europe
$region = 'uk';
break;
case in_array( $country_code, $africa) :
// User is in Africa
$region = 'uk';
break;
case in_array( $country_code, $asia) :
// User is in Asia
$region = 'us';
break;
default :
// User is anywhere else, assume USA
$region = 'us';
break;
}
return $region;
}
/*
* Get the users saved Region
*/
function get_user_region(){
// Get cookie's value
if( isset($_COOKIE['region']) ) {
return $_COOKIE['region'];
} else {
return get_geoip_region();
}
}
/*
* Get the correct homepage ID for set region
*/
/*
function get_region_home_page_id(){
if( get_user_region() == 'us' ) {
$home_page_id = 146;
} else {
$home_page_id = 558;
}
return $home_page_id;
}
*/
/*
* Get the correct homepage ID for set region
*/
/*
function get_region_home_category_id(){
if( get_user_region() == 'us' ) {
$home_category_id = 10;
} else {
$home_category_id = 9;
}
return $home_category_id;
}
*/
/*
* Get the correct menu for set region
*/
/*
function get_region_menu_name(){
if( get_user_region() == 'us' ) {
$name = 'US - Main Menu';
} else {
$name = 'UK - Main Menu';
}
return $name;
}
*/
/*
* Get the correct homepage ID for set region
*/
/*
function get_region_news_category_id(){
if( get_user_region() == 'us' ) {
$news_category_id = 4;
} else {
$news_category_id = 5;
}
return $news_category_id;
}
*/
/*
* Session Tracking (disables cache on Flywheel, so region detect works).
*/
function start_session() {
if( !session_id() ) {
session_start();
}
}
add_action('init', 'start_session', 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment