一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

WordPress 主题更新提示实现方法

时间:2014-05-26 编辑:简简单单 来源:一聚教程网

之前我曾经转载过一个类似的功能,但太复杂了,而且不是很实用,于是还是自己写了一个,支持远程文件的缓存,避免对速度的影响。

首先你要准备一个 JSON 文件,放到你的网站目录里,JSON 文件内容例子:

 代码如下 复制代码
{"Version":"1.0","text":"

这是要给用户说的话

"}

注意,一个值是最新主题的版本,版本是自动和当前用户主题的 style.css 里的版本进行对比,如果用户的主题不是最新版本就在后台的顶部显示第二个属性的内容。

然后在 functions.php 里放下边的代码:

 代码如下 复制代码


$theme_update_json = 'http://www.111com.net /update.json';//注意你的地址
define( 'theme_update_json', $theme_update_json );
 

$themefolder = strtolower( wp_get_theme() );
define( 'theme_folder', $themefolder );
 

$theme_update_json_path = TEMPLATEPATH . '/update.json';
define( 'theme_update_json_path', $theme_update_json_path );
 

function Bing_get_update_json(){
 $fp = @file_get_contents( theme_update_json, 'r' );
 if( !$fp ) return;
 file_put_contents( theme_update_json_path, $fp );
}
add_action( 'theme_' . theme_folder . '_update', 'Bing_get_update_json' );
 

function Bing_theme_version_compare(){
 global $update_json;
 if( !file_exists( theme_update_json_path ) ) return false;
 $theme_data = wp_get_theme();
 $update_json = @file_get_contents( theme_update_json_path, 'r' );
 $update_json = json_decode( $update_json, true );
 if( version_compare( $update_json['Version'], $theme_data['Version'], '>' ) ) return true;
 return false;
}
 
function Bing_update_schedule_event(){
 global $pagenow;
 if( $pagenow == 'themes.php' && isset( $_GET['activated'] ) && !wp_next_scheduled( 'theme_' . theme_folder . '_update' ) ) wp_schedule_event( current_time( 'timestamp' ), 'daily', 'theme_' . theme_folder . '_update' );
}
add_action( 'load-themes.php', 'Bing_update_schedule_event' );
 
function Bing_notices_update(){
 if( !Bing_theme_version_compare() ) return;
 global $update_json;
 echo '

' . $update_json['text'] . '
';
}
add_action( 'admin_notices', 'Bing_notices_update' );

注意第五行修改成你的 JSON 文件地址,每一天去服务器下载一次你的最新 JSON 文件保存到本地

热门栏目