function live_currency_converter_shortcode(){ob_start();
?>
<style>
#currency-converter {
max-width: 400px;
padding: 1.5em;
border: 1px solid #ddd;
border-radius: 12px;
box-shadow: 0 4px 10px rgba(0,0,0,0.05);
font-family: 'Segoe UI', sans-serif;
background: #fff;
}
#currency-converter h3 {
margin-bottom: 1em;
font-size: 1.4em;
color: #333;
}
#currency-converter label {
display: block;
margin-top: 1em;
font-weight: 500;
}
#currency-converter input, #currency-converter select {
width: 100%;
padding: 0.6em;
margin-top: 0.3em;
border: 1px solid #ccc;
border-radius: 6px;
}
#currency-converter button {
margin-top: 1.2em;
width: 100%;
padding: 0.7em;
background-color: #0073e6;
color: white;
border: none;
border-radius: 6px;
font-weight: bold;
cursor: pointer;
}
#currency-converter button:hover {
background-color: #005bb5;
}
#currency-result {
margin-top: 1em;
font-size: 1.1em;
font-weight: 500;
color: #2a2a2a;
}
</style>
<div id="currency-converter">
<h3>Live Currency Converter</h3>
<form onsubmit="return false;">
<label for="amount">Amount</label>
<input type="number" id="amount" placeholder="e.g. 100" required />
<label for="from">From Currency</label>
<select id="from">
<option value="USD">USD</option>
<option value="EUR">EUR</option>
<option value="GBP">GBP</option>
<option value="JPY">JPY</option>
<option value="AUD">AUD</option>
<option value="CAD">CAD</option>
<option value="INR">INR</option>
<option value="ZAR">ZAR</option>
<!-- Add more if needed -->
</select>
<label for="to">To Currency</label>
<select id="to">
<option value="EUR">EUR</option>
<option value="USD">USD</option>
<option value="GBP">GBP</option>
<option value="JPY">JPY</option>
<option value="AUD">AUD</option>
<option value="CAD">CAD</option>
<option value="INR">INR</option>
<option value="ZAR">ZAR</option>
</select>
<button onclick="convertLiveCurrency()">Convert</button>
</form>
<div id="currency-result"></div>
</div>
<script>
async function convertLiveCurrency(){const amount = parseFloat(document.getElementById("amount").value);
const from = document.getElementById("from").value;
const to = document.getElementById("to").value;
if (isNaN(amount)){document.getElementById("currency-result").innerText = "Please enter a valid amount.";
return;
}
const url = `https://api.exchangerate.host/convert?from=${from}&to=${to}&amount=${amount}`;
try {
const res = await fetch(url);
const data = await res.json();
if (data.result){document.getElementById("currency-result").innerText = `${amount} ${from} = ${data.result.toFixed(2)} ${to}`;
} else {
document.getElementById("currency-result").innerText = "Conversion failed. Try again.";
}
} catch (error){document.getElementById("currency-result").innerText = "Error fetching exchange rate.";
}
}
</script>
<?php
return ob_get_clean();
}
add_shortcode('currency_converter', 'live_currency_converter_shortcode');