To Create and Design Unit Converter Tool in PHP that can convert common units (length, weight, temperature, etc.) and be integrated into WordPress. You can convert units like kilometers to miles, grams to pounds, Celsius to Fahrenheit, and more.
1. PHP Code for Unit 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="Unit Converter Tool for converting length, weight, temperature and more."> <meta name="keywords" content="Unit Converter, PHP, Length, Weight, Temperature"> <title>Unit 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; } select, input[type="number"], 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>Unit Converter Tool</h1> <p>Convert units for length, weight, temperature, and more.</p> <form method="post"> <label for="unit-type">Select Conversion Type:</label> <select id="unit-type" name="unit_type" required> <option value="length">Length (Kilometers to Miles)</option> <option value="weight">Weight (Kilograms to Pounds)</option> <option value="temperature">Temperature (Celsius to Fahrenheit)</option> </select> <label for="value">Enter Value:</label> <input type="number" id="value" name="value" step="0.01" placeholder="Enter value" required> <input type="submit" name="convert" value="Convert"> </form> <?php if (isset($_POST['convert'])) { $unitType = $_POST['unit_type']; $value = $_POST['value']; $result = ''; if ($unitType == 'length') { // Kilometers to Miles conversion $result = $value . " kilometers = " . ($value * 0.621371) . " miles"; } elseif ($unitType == 'weight') { // Kilograms to Pounds conversion $result = $value . " kilograms = " . ($value * 2.20462) . " pounds"; } elseif ($unitType == 'temperature') { // Celsius to Fahrenheit conversion $result = $value . " °C = " . (($value * 9/5) + 32) . " °F"; } echo "<div class='result'>$result</div>"; } ?> </div> </body> </html>
2. Integrating the Unit Converter into WordPress
Method 1: Create a Custom Plugin
Steps to Create the Plugin:
- Create a Plugin Folder:
- Go to
/wp-content/plugins/
and create a folder namedunit-converter-tool
.
- Create the Plugin File:
- Inside the
unit-converter-tool
folder, create a file calledunit-converter.php
. - Add the following code in
unit-converter.php
:
<?php /* Plugin Name: Unit Converter Tool Description: A unit converter tool that converts length, weight, and temperature units. Version: 1.0 Author: Your Name */ // Function to display the unit converter tool function unit_converter_tool_shortcode() { ob_start(); // Capture the output ?> <div class="container"> <h1>Unit Converter Tool</h1> <p>Convert units for length, weight, temperature, and more.</p> <form method="post"> <label for="unit-type">Select Conversion Type:</label> <select id="unit-type" name="unit_type" required> <option value="length">Length (Kilometers to Miles)</option> <option value="weight">Weight (Kilograms to Pounds)</option> <option value="temperature">Temperature (Celsius to Fahrenheit)</option> </select> <label for="value">Enter Value:</label> <input type="number" id="value" name="value" step="0.01" placeholder="Enter value" required> <input type="submit" name="convert" value="Convert"> </form> <?php if (isset($_POST['convert'])) { $unitType = $_POST['unit_type']; $value = $_POST['value']; $result = ''; if ($unitType == 'length') { // Kilometers to Miles conversion $result = $value . " kilometers = " . ($value * 0.621371) . " miles"; } elseif ($unitType == 'weight') { // Kilograms to Pounds conversion $result = $value . " kilograms = " . ($value * 2.20462) . " pounds"; } elseif ($unitType == 'temperature') { // Celsius to Fahrenheit conversion $result = $value . " °C = " . (($value * 9/5) + 32) . " °F"; } echo "<div class='result'>$result</div>"; } ?> </div> <?php return ob_get_clean(); // Return the output } // Register the shortcode [unit_converter_tool] add_shortcode('unit_converter_tool', 'unit_converter_tool_shortcode');
- Activate the Plugin:
- Go to Plugins > Installed Plugins in your WordPress Admin Dashboard.
- Find the Unit Converter Tool plugin and click Activate.
- Use the Shortcode:
- You can now place the unit converter on any page or post using the following shortcode:
[unit_converter_tool]
Method 2: Using a PHP Code Plugin
If you don’t want to create a custom plugin, you can use a plugin that allows PHP execution in posts and pages.
Steps:
- Install a PHP Code Execution Plugin:
- Go to Plugins > Add New.
- Search for “Insert PHP Code Snippet” or “PHP Code Snippets”.
- Install and activate the plugin.
- Create a PHP Snippet:
- Go to the PHP Snippets plugin and create a new snippet.
- Paste the Unit Converter Tool code from Step 1 into the snippet.
- Use the Snippet:
- After creating the snippet, you will get a shortcode like
[insert_php_snippet id="1"]
. - Use this shortcode in any page or post where you want the tool to appear.
By following either method, you’ll have a fully functional Unit Converter Tool integrated into your WordPress site that users can use to convert length, weight, and temperature units.