简单的wordpress微信主题切换插件

WordPress插件 4176

wordpress微信主题切换插件可以实现单独指定一个适合微信客户端浏览器访问的wordpress主题,当使用微信打开网站时,自动切换为预先设置的主题,类似于博客吧前面介绍的DW Mobile Switcher插件,适合不是采用响应式设计或没有单独手机站点的wordpress网站。

安装插件:

1、创建一个php文件并命名为weixin.php,把下面的代码添加到该文件,并把代码$theme = ‘Bur’;中的Bur更改为自己微信主题的文件名称

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
/*
Plugin Name: Weixin Switch Theme
Plugin URI: http://fatesinger.com/74958
Description: 切换微信主题
Version: 1.0.0
Author: Bigfa
Author URI: http://fatesinger.com/
*/
if( !function_exists('is_weixin') ) :
function is_weixin(){ 
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
            return true;
    }    
    return false;
}
endif;
function angela_switch_theme($theme){
    if( is_weixin() ){
        $theme = 'Bur';//主题文件夹名而不是主题名
    }
    return $theme;
}
add_filter( 'template', 'angela_switch_theme' );
add_filter( 'stylesheet', 'angela_switch_theme' );
?>

2、把weixin.php文件上传至wp-content/plugins/目录,然后在后台——插件——已安装插件中启用插件,当使用微信客户端打开网站时,就要自动切换为自己设置的主题。

扩展:给所有移动设备指定主题时,可以使用下面的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
/*
Plugin Name: Mobile Switch Theme
Plugin URI: http://fatesinger.com/74958
Description: 切换移动主题
Version: 1.0.0
Author: Bigfa
Author URI: http://fatesinger.com/
*/
 
function angela_switch_theme($theme){
    if( wp_is_mobile() ){
        $theme = 'Bur';//主题文件夹名而不是主题名
    }
    return $theme;
}
add_filter( 'template', 'angela_switch_theme' );
add_filter( 'stylesheet', 'angela_switch_theme' );
?>

代码来自Fatesinger

精品推荐: