Overview
The EVC 5 Day Forecast plugin displays a 5-day weather forecast using NOAA’s weather.gov API. It provides weather data for a specified location, which can be set using a ZIP code. The plugin includes a shortcode for embedding the forecast on any page and a dashboard widget for managing settings.
Features
- 5-Day Forecast Display: Provides weather information for up to five days, including temperature, conditions, precipitation probability, and wind details.
- Dashboard Widget: Allows users to set a ZIP code and refresh weather data directly from the WordPress admin dashboard.
- Shortcode Integration: Embed the forecast on any page using the
[evc_5day_forecast]
shortcode. - Caching: Caches weather data to minimize API calls.
- Responsive Design: Designed to integrate seamlessly into any WordPress theme.
Installation
- Download the plugin.
- Upload the plugin folder to the
/wp-content/plugins/
directory. - Activate the plugin through the ‘Plugins’ menu in WordPress.
- Add the
[evc_5day_forecast]
shortcode to a post or page to display the forecast.
Configuration
Setting the ZIP Code
- Go to the WordPress Dashboard.
- Locate the “EVC 5 Day Forecast Settings” widget.
- Enter a valid ZIP code and click Save & Refresh.
Shortcode Usage
- Add the shortcode
[evc_5day_forecast]
to a post, page, or widget to display the forecast.
Functions
Fetch Weather Data
- Function:
evc_5day_fetch_weather_data($lat, $lon)
- Description: Retrieves weather data from NOAA’s weather.gov API using latitude and longitude.
- Parameters:
$lat
: Latitude (default: McAllen, TX – 26.2034).$lon
: Longitude (default: McAllen, TX – -98.23).
- Returns: Array of weather data or
WP_Error
on failure.
Get Cached Weather Data
- Function:
evc_5day_get_weather_data_cached()
- Description: Retrieves cached weather data or fetches fresh data if the cache is expired.
- Returns: Array of weather data or
null
on failure.
Shortcode Callback
- Function:
evc_5day_forecast_shortcode($atts)
- Description: Generates the HTML for displaying the 5-day forecast.
- Usage:
[evc_5day_forecast]
Dashboard Widget
- Function:
evc_5day_forecast_dashboard_widget()
- Description: Displays the dashboard widget for managing the ZIP code and refreshing data.
Styling
Front-End Styles
- Front-end styles are enqueued via the
evc_5day_forecast_frontend_styles
function. - CSS file:
assets/evc-5day.css
(create this file to customize the appearance).
Admin Styles
- Admin styles can be added via the
evc_5day_forecast_admin_styles
function.
API Usage
- Base Endpoint:
https://api.weather.gov/
- User-Agent: Required as per NOAA guidelines. The plugin uses
(noticas48knvo.com, anthonyacosta@entravision.com)
.
Error Handling
- If weather data cannot be fetched, an error message is displayed.
- Errors are cached for 5 minutes to prevent repeated API calls.
Caching
- Weather data is cached using WordPress transients (
EVC_5DAY_TRANSIENT_DATA
) for 1 hour.
Future Enhancements
- Support for geocoding ZIP codes to latitude/longitude.
- Additional styling options for forecast display.
- Support for more granular weather details (e.g., hourly forecast).
Support
- Author: Anthony Acosta
- Website: anthonyacosta.me
- License: GPL2