Summary: In this post, I'll walk you through a simple solution for fetching data from an API and displaying it on your WordPress website. Don't worry if you're new to PHP; this solution is easy to implement.
The solution is rather simple (if you know php).
Install and activate the “Insert PHP Code Snippet” plugin from the WordPress plugin repository.
Go to your WordPress admin dashboard and navigate to “Snippets” under the “Settings” menu.
Click on “Add New Snippet” and enter a suitable title for your snippet.
In the code editor, enter the following PHP code:
<?php
$json_data = file_get_contents('https://eldolarbtc.com/api');
$data = json_decode($json_data);
if ($data) {
echo "Dolar BTC: " . $data->promedio . "\n";
} else {
echo "Failed to fetch data from the API.";
}
?>
Now, create or edit a WordPress post where you want to display the fetched JSON data.
Add this as a short code block:
[insert_php] your_snippet_title [/insert_php]
Replace your_snippet_title
with the title you provided in step 3.
That's it! You have successfully implemented the solution. When you publish or update the post, it will display the fetched JSON data. The final result should look like this:
Note: If the API request fails, the message "Failed to fetch data from the API" will be displayed instead.