Skip to content

Instantly share code, notes, and snippets.

@takien
Created July 17, 2012 04:38
Show Gist options
  • Save takien/3127185 to your computer and use it in GitHub Desktop.
Save takien/3127185 to your computer and use it in GitHub Desktop.
WordPress mobile version plugin.
<?php
/*
Plugin Name: Takien Mobile Version
Plugin URI: http://takien.com
Description: Automatically detects mobile useragent end redirect to mobile version.
Author: takien
Version: 0.1
Author URI: http://takien.com/
*/
/*edit URL, sesuaikan, no http atau slash
plugin ini work untuk sub domain misalnya m.example.com untuk versi mobile,
caranya, bikin aja sub domain m. di cpanel, arahkan ke public_html
*/
define('NORMAL_URL','www.example.com');
define('MOBILE_URL','m.example.com');
/*MOBILE_THEME_PATH,
sesuaikan dengan path ke mobile theme anda.
contoh WP_CONTENT_DIR.'/nama-theme-mobile-anda/index.php';
sorry saya tidak buat theme, cuma buat plugin :p
*/
define('MOBILE_THEME_PATH',WP_CONTENT_DIR.'/nama-theme-mobile-anda/index.php');
//WAP REDIRECT
$useragent_list = Array(
'2.0 mmp',
'240x320',
'acer',
'alcatel',
'amoi',
'android',
'archos5',
'asus',
'au-mic',
'audiovox',
'avantgo',
'benq',
'bird',
'blackberry',
'blazer',
'cdm',
'cellphone',
'cupcake',
'danger',
'ddipocket',
'docomo',
'dopod',
'dream',
'elaine/3.0',
'ericsson',
'eudoraweb',
'fly',
'haier',
'hiptop',
'hp.ipaq',
'htc',
'huawei',
'i-mobile',
'iemobile',
'incognito',
'ipad',
'iphone',
'ipod',
'j-phone',
'kddi',
'konka',
'kwc',
'kyocera/wx310k',
'lenovo',
'lg',
'lge vx',
'liquid build',
'maemo',
'midp',
'midp-2.0',
'mmef20',
'mmp',
'mobilephone',
'mot',
'mot-v',
'motorola',
'netfront',
'newgen',
'newt',
'nexus one',
'nintendo ds',
'nintendo wii',
'nitro',
'nokia',
'novarra',
'o2',
'openweb',
'opera mini',
'opera mobi',
'opera.mobi',
'palm',
'panasonic',
'pantech',
'pdxgw',
'pg',
'philips',
'phone',
'playstation portable',
'portalmmm',
'ppc',
'proxinet',
'psp',
'pt',
'qtek',
'sagem',
'samsung',
'sanyo',
'sch',
'sec',
'sendo',
'series60.*webkit',
'series60/5.0',
'sgh',
'sharp',
'small',
'smartphone',
'softbank',
'sonyericsson',
'sph',
'symbian',
'symbianos',
't-mobile mytouch 3g',
't-mobile opal',
'tattoo',
'toshiba',
'treo',
'ts21i-10',
'up.browser',
'up.link',
'uts',
'vertu',
'vodafone',
'wap',
'webmate',
'webos',
'willcome',
'windows ce',
'windows.ce',
'winwap',
'xda',
'zte'
);
session_start();
$useragent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : false;
$current_url = $_SERVER["HTTP_HOST"];
$mobile = false;
foreach($useragent_list as $u){
$pattern = preg_quote($u);
if(preg_match("~$pattern~i",$useragent)){
$mobile = true;
}
}
if(is_admin()){
$mobile = false;
}
function current_url() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
function is_mobile(){
global $mobile;
if($mobile) return true;
}
if((!isset($_SESSION['mobile_version'])) && ($current_url == MOBILE_URL) && !isset($_GET['mobile'])) {
if(isset($_SESSION['full'])){
unset($_SESSION['full']);
}
header("Location: http://".MOBILE_URL."/?mobile=1");
}
if(isset($_GET['mobile']) && !$_SESSION['mobile_version']){
$_SESSION['mobile_version'] = true;
}
if(isset($_GET['full']) && $_SESSION['mobile_version']){
$_SESSION['mobile_version'] = false;
}
if(is_mobile()){
if(isset($_GET['full'])){
$_SESSION['full'] = true;
}
if(($_SESSION['on_mobile']) && ($current_url == NORMAL_URL)) {
unset($_SESSION['on_mobile']);
}
if((!$_SESSION['on_mobile']) && ($current_url == MOBILE_URL)) {
$_SESSION['on_mobile'] = true;
}
if((!$_SESSION['on_mobile']) && ($current_url !== MOBILE_URL) && (!isset($_GET['full'])) && ($_SESSION['full'] == false)){
$requesturi = $_SERVER['REQUEST_URI'];
$newlocation = add_query_arg(Array('mobile'=>1),"http://".MOBILE_URL."$requesturi");
header("Location: $newlocation");
die();
}
}
function mobile_uri($mobileuri=''){
$mobileuri = $mobileuri ? $mobileuri : 'http://'.MOBILE_URL.$_SERVER['REQUEST_URI'];
$newlocation = remove_query_arg( 'full', $mobileuri );
$newlocation = add_query_arg(Array('mobile'=>1),$newlocation);
return $newlocation;
}
function full_uri($fulluri=''){
$fulluri = $fulluri ? $fulluri : 'http://'.NORMAL_URL.$_SERVER['REQUEST_URI'];
$newlocation = remove_query_arg( 'mobile', $fulluri );
$newlocation = add_query_arg(Array('full'=>1),$newlocation);
return $newlocation;
}
function redirect_to_wap() {
include(MOBILE_THEME_PATH);
exit;
}
if((($current_url == MOBILE_URL) && ($_SESSION['on_mobile'])) || ($current_url == MOBILE_URL)){
if(!isset($_GET['full'])){
add_action('template_redirect', 'redirect_to_wap');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment