Using the template tag is the most flexible way to get your API data and is preferred over the shortcode.
The easiest way to test your API is to create a new page within WordPress and load the template tag within the page template. You will need to have FTP access or some other type of file manager access to be able to upload a template file. Here are the steps to do that:
Create a new page
- In the WordPress admin, go to Pages > Add New and create a page named Test API
- It’s important that you name the page exactly Test API – the template that we create will rely on it being named this
Create a template file
- In your WordPress theme, copy the index.php template file and rename the new file to page-test-api.php
- Delete everything in the new file and add the below code into the page-test-api.php template file
- Save the file
- Upload this new file ensuring that the file is in the root directory of your theme
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) { exit; }
// do our header
get_header();
// call our API (make sure API is set up in admin area)
$random = wpgetapi_endpoint( 'quotable', 'random', array('debug' => false) );
// if there is no data from the API
if( ! $random || $random == 'null' ) {
echo 'No result. Ensure you have the API set up correctly following the guide here - https://wpgetapi.com/docs/quick-start-guide/';
// if there is data from API, output it
} else {
// simply dumping the data.
// use the $random variable however you like - PHP knowledge required.
var_dump( $random );
}
// do our footer
get_footer();
?>
In the above code, the ‘debug’ option is set to false, but you can change this to true and you will see a whole bunch of debug information about the API.
View the page
When you now view the Test API page on the front of your website, you should see the output of the Quotable API (or the ‘no result’ error message) and it should look something like in the image below.
NOTE: Within the Quotable API endpoint that you set up earlier, you can also try setting the Results Format field to PHP array data and the output will look slightly different and allow you to work with array data rather than a JSON string.