Weather in WordPress

There are cases, when need output one or more weather parameters e.g. temperature or wind speed and direction or set custom styles for weather parameters. In this case, install plugin will not solve the problem and good solution will be write a small function.

Simple weather in WordPress (without plugin)

Problem: Output weather parameters without plugin in WordPress.

Solution: Firstly, we should be get API key from one of weather services openweathermap.org, weather.com, etc.

Step 1:

<?php
// Get Weather For 1 Day From openweathermap.org
function get_weather( $coordinates ) {
	$API_key = '111222333444555';
	$json = file_get_contents( 'http://api.openweathermap.org/data/2.5/weather?&units=imperial&lat=' . $coordinates['lat'] . '&lon=' . $coordinates['lng'] . '&appid=' . $API_key );
		$data = json_decode( $json, true );
		
		$weather = [
			'temperature' => round( $data['main']['temp'], 0 ),
			'description' => ucfirst( $data['weather'][0]['description'] ),
			'icon' => 'http://openweathermap.org/img/wn/' . $data['weather'][0]['icon'] . '@2x.png',
		];
		
		return $weather;
}

// Get Weather For 5 Days From openweathermap.org
function get_weather( $coordinates, $days ) {
	$API_key = '111222333444555';
	$json = file_get_contents( 'https://api.openweathermap.org/data/2.5/onecall?&units=imperial&lat=' . $coordinates['lat'] . '&lon=' . $coordinates['lng'] . '&exclude=minutely,hourly&appid=' . $API_key );
	$data = json_decode( $json, true );
	$days = ( is_int( $days ) ) ? $days : (int)$days;
		
	$weather = [
		'today' => [
			'date' => $data['daily'][0]['dt'],
			'temp' => round( $data['current']['temp'], 0 ),
			'description' => $data['current']['weather'][0]['description'],
			'icon' => 'http://openweathermap.org/img/wn/' . $data['current']['weather'][0]['icon'] . '@2x.png',
		],
	];
		
	if ( $days ) {
		for ( $i = 1; $i <= $days; $i++ ) {
			$weather[ $i . '-day' ] = [
				'date' => $data['daily'][ $i ]['dt'],
				'temp' => round( $data['daily'][ $i ]['temp'], 0 ),
				'description' => $data['daily'][ $i ]['weather'][0]['description'],
				'icon' => 'http://openweathermap.org/img/wn/' . $data['daily'][ $i ]['weather'][0]['icon'] . '@2x.png',
			];
		}
	}
		
	return $weather;
}

Unit of measurement:

units=imperial — Fahrenheit
units=metric — Celsius

Sources:

  1. https://stackoverflow.com/questions/31463712/php-get-data-openweathermap
  2. https://openweathermap.org/current
  3. https://phppot.com/php/forecast-weather-using-openweathermap-with-php/

Leave Comment

2 Replies to “Simple weather in WordPress (without plugin)”

  • Tim says:

    For those of us with little coding skills what’s the next step to actually display the weather on a page or the header of the site?

    Thanks

    • Admin says:

      I didn’t understand your question.
      If you use one of the functions, you get $weather array. To view content you can use var_dump( $weather ); and after then write simple code for parse results use for example foreach loop.
      If you have any question, don’t be so shy and ask me.

Leave a Reply to Admin Cancel reply

Your email address will not be published. Required fields are marked *