Skip to content

Instantly share code, notes, and snippets.

@debabratakarfa
Last active July 16, 2017 12:19
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 debabratakarfa/ba8f24bab1f69e99f46ab0122a2f0d97 to your computer and use it in GitHub Desktop.
Save debabratakarfa/ba8f24bab1f69e99f46ab0122a2f0d97 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Custom Theme Switcher
* Description: Handles Mobile Theme Switching
* Version: 1.0.1
*/
add_filter( 'template', 'custom_theme_redirect' );
add_filter( 'option_template', 'custom_theme_redirect' );
add_filter( 'option_stylesheet', 'custom_theme_redirect' );
function custom_get_subdomain() {
$subdomain = '';
if ( ! empty( $_SERVER['HTTP_HOST'] ) ) {
$host_tokens = explode( '.', $_SERVER['HTTP_HOST'] );
if ( ! empty( $host_tokens ) ) {
return strval( $host_tokens[0] );
}
}
return $subdomain;
}
function custom_theme_redirect( $theme ) {
if ( ! is_admin() ) {
if ( isset( $_GET['mobile'] ) ) {
setcookie( 'mobile', true, time() + 1, '/', CUSTOM_ROOT_DOMAIN );
setcookie( 'nomobile', false, time() -1, '/', CUSTOM_ROOT_DOMAIN );
$_COOKIE['mobile'] = true;
$_COOKIE['nomobile'] = false;
}
if ( DOMAIN_CURRENT_SITE === CUSTOM_MOBILE_DOMAIN || isset( $_COOKIE['mobile'] ) ) {
if ( isset( $_GET['nomobile'] ) ) {
setcookie( 'mobile', false, time() -1, '/', CUSTOM_ROOT_DOMAIN );
setcookie( 'nomobile', true, time() + 1, '/', CUSTOM_ROOT_DOMAIN );
$url = 'http://' . CUSTOM_MASTER_DOMAIN . $_SERVER['REQUEST_URI'];
wp_safe_redirect( $url );
exit;
}
$theme = 'custom-mobile';
} elseif ( DOMAIN_CURRENT_SITE === CUSTOM_MASTER_DOMAIN ) {
include_once( 'Mobile_Detect.php' );
$detect = new Mobile_Detect;
if ( CUSTOM_AUTO_MOBILE_REDIRECT && $detect->isMobile() && ! $detect->isTablet() ) {
if ( ! isset( $_COOKIE['nomobile'] ) || ! $_COOKIE['nomobile'] ) {
$url = 'http://' . CUSTOM_MOBILE_DOMAIN . $_SERVER['REQUEST_URI'];
wp_safe_redirect( $url );
exit;
}
}
}
}// End if().
return $theme;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment