WordPress に今日の天気ウィジェットを追加
5月 28th, 2009
今日の天気をサイドバーに表示したかったから、
なるたけシンプルに。
wp-content/plugins/akismet/akismet.php のソースと
天気情報を取得できるAPIを使ってみよう – PHPプロ!TIPS+
http://www.phppro.jp/phptips/archives/vol51/1Weather Hacks – livedoor 天気情報
http://weather.livedoor.com/weather_hacks/
ここを参考にして、自分用に今日の天気ウィジェットを作ってみた。
祝、初 PHP。
wp-content/plugins/weather.php
<?php
/*
Plugin Name: Weather
Plugin URI: http://0154.jp/
Description: Weather Hacks から今日の天気を表示
Version: 0.0.1
Author: かじゅ
Author URI: http://0154.jp/
*/
function widget_weather_register() {
if ( function_exists('register_sidebar_widget') ) :
function widget_weather($args) {
extract($args);
?>
<?php echo $before_widget; ?>
<?php echo $before_title . '今日の天気' . $after_title; ?>
<?php
$city = "11";
$day = "today";
$base_url = "http://weather.livedoor.com/forecast/webservice/rest/v1?city=" . $city . "&day=" . $day;
$xml = simplexml_load_file($base_url);
$location = $xml->location->attributes();
echo '<div style="padding:10px">' . $location["pref"] . ' ' . $location["city"];
echo ' ' . date("m/j(D)", strtotime($xml->forecastdate)) . '<br />';
echo '<a href="' . $xml->link . '" title="' . $xml->copyright->title . '">';
echo '<img style="float:left;margin:5px;" src="' . $xml->image->url . '" alt="' . $xml->image->title . '" width="' . $xml->image->width . '" height="' . $xml->image->height . '" />';
echo '</a>';
echo $xml->telop . '<br />';
$temp = $xml->temperature->max->celsius;
$temp = ($temp) ? $temp : '--';
echo '<span style="color:#CC0000">最高 ' . $temp . ' ℃</span> / ';
$temp = $xml->temperature->min->celsius;
$temp = (0 < strlen($temp)) ? $temp : '--';
echo '<span style="color:#0000CC">最低 ' . $temp . ' ℃</span>';
echo '</div>';
?>
<?php echo $after_widget; ?>
<?php
}
register_sidebar_widget('Weather', 'widget_weather', null, 'weather');
endif;
}
add_action('init', 'widget_weather_register');
?>