之前我曾經轉載過一個類似的功能,但太復雜了,而且不是很實用,于是還是自己寫了一個,支持遠程文件的緩存,避免對速度的影響.
首先你要準備一個 JSON 文件,放到你的網站目錄里,JSON 文件內容例子,代碼如下:
{"Version":"1.0","text":"<p>這是要給用戶說的話</p>"}
注意,一個值是最新主題的版本,版本是自動和當前用戶主題的 style.css 里的版本進行對比,如果用戶的主題不是最新版本就在后臺的頂部顯示第二個屬性的內容,然后在 functions.php 里放下邊的代碼:
- $theme_update_json = 'http://www.49028c.com /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 '<div id="message" class="updated fade">' . $update_json['text'] . '</div>';
- }
- add_action( 'admin_notices', 'Bing_notices_update' );
注意修改成你的 JSON 文件地址,每一天去服務器下載一次你的最新 JSON 文件保存到本地.
新聞熱點
疑難解答
圖片精選