最近在给客户开发主题的过程中,遇到了一项需求:对手机设备采用不同的主题以及不同的域名,两个站点同步更新。手机移动设备采用单独的主题在WEUI主题的制作过程中已经使用过相关的技术,但是当时手机和电脑是同一个站点,经过和客户沟通确认,技术层面实现方式如下:采用同一个站点,通过判断用户的UA,绑定不同的域名,调用不同的主题。
相关代码如下,使用方法:(写成插件)
//针对不同的设备起用不同的域名
class themetuts_theme_and_domain_switch{
private $mobile_domain;
private $pc_domain;
private $site_url;
private $request;
private $current_domain;
public function __construct(){
$current_domain = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
$current_domain .= '://'.$_SERVER['HTTP_HOST'];
$this->mobile_domain = 'http://m.web.themetuts.com';
$this->pc_domain = 'http://web.themetuts.com';
$this->site_url = site_url();
$this->request = $_SERVER['REQUEST_URI'];
$this->current_domain = $current_domain;
add_action('init', array($this, 'tw_switch_mobile_domain'));
}
public function tw_switch_mobile_domain(){
if(wp_is_mobile()){
if($this->current_domain != $this->mobile_domain){
wp_redirect($this->mobile_domain . ':80' . $this->request);
exit;
}
define('WP_HOME', $this->mobile_domain);
define('WP_SITEURL', $this->mobile_domain);
}else{
if($this->current_domain != $this->pc_domain){
wp_redirect($this->pc_domain . ':80' . $this->request);
exit;
}
define('WP_HOME', $this->pc_domain);
define('WP_SITEURL', $this->pc_domain);
}
}
}
new themetuts_theme_and_domain_switch();
//根据浏览器对主题进行切换,设置的是主题文件夹的名称
function themetuts_theme_switch($theme){
if( wp_is_mobile() ){
$theme = 'twentyseventeen';
}else{
$theme = 'Project';
}
return $theme;
}
add_filter( 'template', 'themetuts_theme_switch' );
add_filter( 'stylesheet', 'themetuts_theme_switch' );
感谢您的打赏,我们会更加努力的更新站点!
本文链接:WordPress不同的User Agent启用不同域名以及不同主题
转载声明:本站文章若无特别说明,皆为原创,转载请注明来源:刘荣焕,谢谢!