Why Use functions.php Instead of a Plugin?
By adding custom code to your functions.php
file, you can integrate external open third-party APIs without relying on third-party plugins. This ensures better control over your site’s performance and security.
Step 1: Obtain the API Key and Endpoint
To integrate an API, you need:
- The API endpoint (URL) from the provider.
- Your API key (if required).
- The parameters needed for the API request (check API documentation).
For example, let’s use OpenWeatherMap API to get weather data.
Step 2: Add the Code to functions.php
Open your WordPress theme’s functions.php
file (use child theme for best practice) and add the following code:
function get_weather_data() {
$api_key = 'YOUR_API_KEY_HERE'; // Replace with your OpenWeatherMap API key
$city = 'Stockholm'; // Change city as needed
$url = "https://api.openweathermap.org/data/2.5/weather?q={$city}&appid={$api_key}&units=metric";
$response = wp_remote_get($url);
if (is_wp_error($response)) {
return 'Error fetching weather data.';
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (!$data || empty($data['main'])) {
return 'No weather data available.';
}
$temperature = $data['main']['temp'];
$humidity = $data['main']['humidity'];
$condition = $data['weather'][0]['description'];
return "Stockholm Weather: Temperature: {$temperature}°C, Humidity: {$humidity}%, Condition: {$condition}";
}
function display_weather_shortcode() {
return get_weather_data();
}
add_shortcode('weather_data', 'display_weather_shortcode');
Step 3: Use the Shortcode to Display Data
Now, go to Pages → Add New and insert this shortcode:
[weather_data]
Publish the page and check the weather data displayed on your site!
Adding Exchange Rate API Integration
Another useful API integration is fetching exchange rates. Let’s integrate the ExchangeRate-API to get the conversion rate from USD to EUR.
Step 1: Get API Key
Sign up at ExchangeRate-API and get your free API key.
Step 2: Add the Following Code to Your functions.php
function get_exchange_rate() {
$api_key = 'YOUR_API_KEY_HERE'; // Replace with your ExchangeRate-API key
$url = "https://v6.exchangerate-api.com/v6/{$api_key}/latest/USD";
$response = wp_remote_get($url);
if (is_wp_error($response)) {
return 'Error fetching exchange rate data.';
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (!$data || empty($data['conversion_rates']['EUR'])) {
return 'No exchange rate data available.';
}
$rate = $data['conversion_rates']['EUR'];
return "Exchange Rate (USD to EUR): 1 USD = {$rate} EUR";
}
function display_exchange_rate_shortcode() {
return get_exchange_rate();
}
add_shortcode('exchange_rate', 'display_exchange_rate_shortcode');
Step 3: Use the Shortcode to Display Exchange Rate
Place this shortcode anywhere on your WordPress site:
[exchange_rate]
Now, the latest exchange rate for USD to EUR will be displayed dynamically.
Adapting This Method for Any API
If you want to use a different API:
- Replace the API endpoint URL.
- Update the parameters in the URL.
- Modify the response parsing (extract needed data fields).
- You can save this custom code as a snippet using any snippet plugin if you’re not familiar with the
functions.php
file. - Use a child theme and give this code there for best practice
Conclusion
By following these steps, you can easily integrate any third-party API into your WordPress site without using a plugin. Whether you need live weather data or real-time exchange rates, this approach helps display dynamic data efficiently. Try it out and enhance your WordPress site with useful real-time information!
Need Help?
Want help deciding or need a custom site built? Let’s talk!
Liked this post? Share it with your team or on LinkedIn to help more creators build inclusive websites!