Here is a Currency Converter Tool using PHP that allows users to convert between two currencies using exchange rates from an external API like exchangerate-api.com
.
1. PHP Code for Currency Converter Tool
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="description" content="Simple Currency Converter Tool to convert between currencies."> <meta name="keywords" content="Currency Converter, PHP, Currency Exchange"> <title>Currency Converter Tool</title> <style> body { font-family: Arial, sans-serif; background-color: #f4f4f4; margin: 0; padding: 20px; } .container { max-width: 600px; margin: 0 auto; padding: 20px; background-color: #fff; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } h1 { text-align: center; } form { margin-top: 20px; } label { display: block; margin-bottom: 5px; font-weight: bold; } input[type="text"], select, input[type="submit"] { width: 100%; padding: 10px; margin-bottom: 20px; border: 1px solid #ccc; border-radius: 5px; } input[type="submit"] { background-color: #007bff; color: #fff; cursor: pointer; } .result { font-size: 18px; text-align: center; color: #333; } .error { color: red; text-align: center; } </style> </head> <body> <div class="container"> <h1>Currency Converter Tool</h1> <p>Enter the amount and select currencies to convert.</p> <form method="post"> <label for="amount">Amount:</label> <input type="text" id="amount" name="amount" placeholder="Enter amount" required> <label for="from_currency">From:</label> <select id="from_currency" name="from_currency" required> <option value="USD">USD - United States Dollar</option> <option value="EUR">EUR - Euro</option> <option value="GBP">GBP - British Pound</option> <option value="INR">INR - Indian Rupee</option> <option value="PKR">PKR - Pakistani Rupee</option> <!-- Add more currencies as needed --> </select> <label for="to_currency">To:</label> <select id="to_currency" name="to_currency" required> <option value="USD">USD - United States Dollar</option> <option value="EUR">EUR - Euro</option> <option value="GBP">GBP - British Pound</option> <option value="INR">INR - Indian Rupee</option> <option value="PKR">PKR - Pakistani Rupee</option> <!-- Add more currencies as needed --> </select> <input type="submit" name="convert_currency" value="Convert"> </form> <?php if (isset($_POST['convert_currency'])) { $amount = $_POST['amount']; $from_currency = $_POST['from_currency']; $to_currency = $_POST['to_currency']; // Validate input if (!is_numeric($amount) || $amount <= 0) { echo "<div class='error'>Please enter a valid amount.</div>"; } else { // API URL for currency conversion $api_url = "https://v6.exchangerate-api.com/v6/YOUR-API-KEY/latest/{$from_currency}"; // Get the conversion rates $response = @file_get_contents($api_url); if ($response) { $data = json_decode($response, true); // Check if the API call was successful if ($data['result'] == 'success') { // Get the exchange rate for the selected currency $conversion_rate = $data['conversion_rates'][$to_currency]; // Calculate the converted amount $converted_amount = $amount * $conversion_rate; // Display the result echo "<div class='result'>"; echo "<p><strong>Converted Amount:</strong> $converted_amount $to_currency</p>"; echo "</div>"; } else { echo "<div class='error'>Unable to fetch currency data. Please try again later.</div>"; } } else { echo "<div class='error'>Error fetching data from the API. Please check your connection or try again later.</div>"; } } } ?> </div> </body> </html>
2. How the Currency Converter Tool Works
- Input:
- Users enter the amount they want to convert and select the currencies they want to convert from and to.
- Processing:
- The PHP script calls an external API (e.g.,
exchangerate-api.com
) to get the exchange rates. You need to replaceYOUR-API-KEY
with a valid API key from the service. - Output:
- Displays the converted amount based on the current exchange rates.
3. How to Get an API Key for Exchange Rates
- Visit the ExchangeRate-API website and sign up for a free account.
- You will receive an API key that you can use in the URL for fetching exchange rates.
Example URL with API Key:
https://v6.exchangerate-api.com/v6/YOUR-API-KEY/latest/USD
4. Integrating the Currency Converter Tool into WordPress
Method 1: Creating a Custom Plugin
Steps:
- Create a Plugin Folder:
- Go to
/wp-content/plugins/
and create a folder calledcurrency-converter-tool
.
- Create the Plugin File:
- Inside the
currency-converter-tool
folder, create a file namedcurrency-converter.php
. - Add the following PHP code:
<?php /* Plugin Name: Currency Converter Tool Description: A simple tool to convert currencies using real-time exchange rates. Version: 1.0 Author: Your Name */ // Function to display the Currency Converter Tool function currency_converter_tool_shortcode() { ob_start(); // Start output buffering ?> <div class="container"> <h1>Currency Converter Tool</h1> <p>Enter the amount and select currencies to convert.</p> <form method="post"> <label for="amount">Amount:</label> <input type="text" id="amount" name="amount" placeholder="Enter amount" required> <label for="from_currency">From:</label> <select id="from_currency" name="from_currency" required> <option value="USD">USD - United States Dollar</option> <option value="EUR">EUR - Euro</option> <option value="GBP">GBP - British Pound</option> <option value="INR">INR - Indian Rupee</option> <option value="PKR">PKR - Pakistani Rupee</option> <!-- Add more currencies as needed --> </select> <label for="to_currency">To:</label> <select id="to_currency" name="to_currency" required> <option value="USD">USD - United States Dollar</option> <option value="EUR">EUR - Euro</option> <option value="GBP">GBP - British Pound</option> <option value="INR">INR - Indian Rupee</option> <option value="PKR">PKR - Pakistani Rupee</option> <!-- Add more currencies as needed --> </select> <input type="submit" name="convert_currency" value="Convert"> </form> <?php if (isset($_POST['convert_currency'])) { $amount = $_POST['amount']; $from_currency = $_POST['from_currency']; $to_currency = $_POST['to_currency']; // Validate input if (!is_numeric($amount) || $amount <= 0) { echo "<div class='error'>Please enter a valid amount.</div>"; } else { // API URL for currency conversion $api_url = "https://v6.exchangerate-api.com/v6/YOUR-API-KEY/latest/{$from_currency}"; // Get the conversion rates $response = @file_get_contents($api_url); if ($response) { $data = json_decode($response, true); // Check if the API call was successful if ($data['result'] == 'success') { // Get the exchange rate for the selected currency $conversion_rate = $data['conversion_rates'][$to_currency]; // Calculate the converted amount $converted_amount = $amount * $conversion_rate; // Display the result echo "<div class='result'>"; echo "<p><strong>Converted Amount:</strong> $converted_amount $to_currency</p>"; echo "</div>"; } else { echo "<div class='error'>Unable to fetch currency data. Please try again later.</div>"; } } else { echo "<div class='error'>Error fetching data from the API. Please check your connection or try again later.</div>"; } } } ?> </div> <?php return ob_get_clean(); // Return the output } // Register the shortcode [currency_converter_tool] add_shortcode('currency_converter_tool', 'currency_converter_tool_shortcode'); ?>
- Activate the Plugin:
- Go to the WordPress Admin Dashboard > Plugins and activate the Currency Converter Tool plugin.
- Add the Shortcode:
- To display the tool on any post or page, use the shortcode:
[currency_converter_tool]
This Currency Converter Tool is simple and useful for anyone looking to quickly convert currencies.