PRO: Functions to Grab Weather Data

My plugin has provided several functions that are helpful when making your own custom templates or just grabbing weather data to show in various spots around your website. In version 1.1.9 these functions were improved and some new ones were added. These functions utilize your WordPress transient caching system so they should improve performance once the data has been cached. Here is a breakdown of the functions available to you. You can click the function name to learn more and get a code snippet.

Function
the_awesome_weather_widget
get_awesome_weather_widget
get_awesome_weather_openweathermaps
get_awesome_weather_yahoo
awesome_weather_get_woeid
awesome_weather_widget_by_latlong_openweathermaps($lat,$lon,$template)
awesome_weather_widget_by_latlong_yahoo( $lat,$lon,$template)
awesome_weather_by_latlong_openweathermaps($lat,$lon)
awesome_weather_by_latlong_yahoo($lat,$lon)
awesome_weather_preset_condition_names_openweathermaps($condition_code)
awesome_weather_preset_condition_names_yahoo($condition_code)
awe_get_appid
awe_get_long_lat_city_id($weather,$what_to_get)
awe_c_to_f( $c )
awe_f_to_c( $f )

the_awesome_weather_widget

Quickly show a weather widget with this nice handy ACF style function. You can pass in one variable as a string, array or object.

// STRING
the_awesome_weather_widget('Edmond, OK');

// ARRAY
the_awesome_weather_widget( array('location' => 'Edmond, OK', 'size' => 'material' ) );

// OBJECT
$settings = new stdclass;
$settings->location = "Edmond, OK";
$settings->size = "material";
the_awesome_weather_widget( $settings );

get_awesome_weather_widget

Same data and function as the_awesome_weather_widget only instead of echoing the widget it returns the widget.

// STRING
echo get_awesome_weather_widget('Edmond, OK');

// ARRAY
$widget = get_awesome_weather_widget( array('location' => 'Edmond, OK', 'size' => 'material' ) );
echo $widget;

// OBJECT
$settings = new stdclass;
$settings->location = "Edmond, OK";
$settings->size = "material";
$weather = get_awesome_weather_widget( $settings );

get_awesome_weather_openweathermaps

Grab just the weather data from OpenWeatherMap City ID. It accepts a string, integer (OPENWEATHERMAP CITY ID), array or object. Help guide on how to find your city id.

// STRING
$weather = get_awesome_weather_openweathermaps( 'London' );

// INTEGER
$weather = get_awesome_weather_openweathermaps( 2643743 );

// OBJECT
$where = new stdclass;
$where->location = "London, UK";
$weather = get_awesome_weather_openweathermaps( $where );

// ARRAY (Multiple Locations)
$locals = get_awesome_weather_openweathermaps( array( 2643743, 'Paris', 'Madrid') );

get_awesome_weather_yahoo

Grab just the weather data from Yahoo!. It accepts a string, integer (WOEID), array or object. Here is a WOEID Lookup tool.

// STRING
$weather = get_awesome_weather_yahoo( 'London' );

// WOEID
$weather = get_awesome_weather_yahoo( 44418 );

// OBJECT
$where = new stdclass;
$where->location = "London, UK";
$weather = get_awesome_weather_yahoo( $where );

// ARRAY (Multiple Locations)
$locals = get_awesome_weather_yahoo( array( 2643743, 'Paris', 'Madrid') );

awesome_weather_get_woeid

Grab a Yahoo! woeid from a string location. It will return the first location it finds.

$woeid = awesome_weather_get_woeid( 'Edmond, OK' );

awesome_weather_widget_by_latlong_openweathermaps($lat,$lon,$template)

Grab a weather widget based on a Latitude and Longitude location.

$widget = awesome_weather_widget_by_latlong_openweathermaps(48.856614,2.352222,'material');

awesome_weather_widget_by_latlong_yahoo( $lat,$lon,$template)

Grab a weather widget based on a Latitude and Longitude location.

$widget = awesome_weather_widget_by_latlong_yahoo(48.856614,2.352222,'micro');

awesome_weather_by_latlong_openweathermaps($lat,$lon)

Grab the weather data for a Latitude and Longitude location.

$weather_data = awesome_weather_by_latlong_openweathermaps(48.856614,2.352222);

awesome_weather_by_latlong_yahoo($lat,$lon)

Grab the weather data for a Latitude and Longitude location.

$weather_data = awesome_weather_by_latlong_yahoo(48.856614,2.352222);

awesome_weather_preset_condition_names_openweathermaps($condition_code)

Get a text string of current weather conditions. Here is a list of the available options. If nothing can be found it will return default.

$weather = get_awesome_weather_openweathermaps( 'London' );

// CURRENT CONDITION STRING: sunny, cloudy, snow, etc.
echo awesome_weather_preset_condition_names_openweathermaps($weather->data['current']['condition_code']);

awesome_weather_preset_condition_names_yahoo($condition_code)

Get a text string of current weather conditions. Here is a list of the available options. If nothing can be found it will return default.

$weather = get_awesome_weather_yahoo( 'London' );

// CURRENT CONDITION STRING: sunny, cloudy, snow, etc.
echo awesome_weather_preset_condition_names_yahoo($weather->data['current']['condition_code']);

awe_get_appid

If for whatever you reason you want to get the OpenWeatherMap APP ID that you (or your client) has entered to make a custom call to their API. You can grab it with this function. It is smart enough to check for the wp-config first then the option saved in the database.

// GET KEY
$appkey = awe_get_appid();

// ... MAKE CUSTOM CALL TO OPENWEATHERMAP
$weather = wp_remote_get( "http://api.openweathermap.org/data/2.5/weather?id=345345&lang=sp&units=metric&APPID=" .  $appkey);

awe_get_long_lat_city_id($weather,$what_to_get)

Get the City ID (for whichever provider) based on a Latitude and Longitude location. The first $weather attribute needs to be an object. And the $what_to_get has two options: just the id or a little object about the location (which includes the id, but also has a city, state, etc.)

// CREATE AN OBJECT
$location = new stdclass;
$location->provider = 'yahoo'; // OR openweathermaps
$location->longlat_location = '48.856614,2.352222';

// GET CITY ID
$city_id = awe_get_long_lat_city_id($location);

// GET OBJECT ABOUT CITY
$city_data = awe_get_long_lat_city_id($location, 'data');

awe_c_to_f( $c )

If you want to convert a temperature from celsius to fahrenheit.

$current_temp = awe_c_to_f( $weather->data['current']['temp'] );

awe_f_to_c( $f )

If you want to convert a temperature from fahrenheit to celsius.

$current_temp = awe_f_to_c( $weather->data['current']['temp'] );