Weather Forecast
// Add Shortcode
function custom_shortcode() {
function display_weather_info_shortcode() {
$api_key = '28c6abd883594795a8565602232303';
$api_url = 'https://api.weatherapi.com/v1/current.json';
$location = get_user_location();
if (!$location) {
return 'Konum bilgisi alınamadı.';
}
$params = array(
'key' => $api_key,
'q' => $location['latitude'] . ',' . $location['longitude'],
'lang' => 'tr',
);
$response = wp_remote_get(add_query_arg($params, $api_url));
if (is_wp_error($response)) {
return 'Hava durumu bilgileri alınamadı.';
}
$data = json_decode(wp_remote_retrieve_body($response), true);
$output = '<div class="weather-info">';
$output .= '<div class="city-name">' . $data['location']['name'] . '</div>';
$output .= '<div class="weather-details">';
$output .= '<div class="weather-icon"><img src="' . $data['current']['condition']['icon'] . '" alt="' . $data['current']['condition']['text'] . '"></div>';
$output .= '<div class="temperature">' . $data['current']['temp_c'] . '°C</div>';
$output .= '<div class="weather-description">' . $data['current']['condition']['text'] . '</div>';
$output .= '<div class="wind-speed">Rüzgar: ' . $data['current']['wind_kph'] . ' km/s</div>';
$output .= '</div>';
$output .= '</div>';
return $output;
}
add_shortcode( 'display_weather_info', 'display_weather_info_shortcode' );
}
add_shortcode( 'Weather Forecast', 'custom_shortcode' );