function fetch_nexusrealms_feed() {
// Define the API URL
$api_url = ‚http://api.nexusrealms.de:4000/api/801/feed‘;
// Define a unique transient name
$transient_name = ’nexusrealms_feed‘;
// Check if the transient exists and is not expired
if (false === ($feed = get_transient($transient_name))) {
// Transient does not exist or has expired, fetch the data
$response = wp_remote_get($api_url);
// Check for errors
if (is_wp_error($response)) {
return ‚Error fetching data from Nexus Realms API.‘;
}
// Get the body of the response
$body = wp_remote_retrieve_body($response);
// Decode the JSON response
$data = json_decode($body, true);
// Check if the data is not empty
if (!empty($data)) {
// Get the newest feed item
$newest_feed = $data[0];
// Extract the message in English
$message = $newest_feed[‚message‘][‚en‘];
// Set the transient with the fetched data and an expiration time of 60 seconds
set_transient($transient_name, $message, 60);
// Return the message
return $message;
} else {
return ‚No data available from Nexus Realms API.‘;
}
} else {
// Transient exists and is not expired, return the cached data
return $feed;
}
}
// Register the shortcode
add_shortcode(’nexusrealms_feed‘, ‚fetch_nexusrealms_feed‘);