Important: We will use the ‘Quotable’ API as an example and this needs to be set up in the admin first – here is a quick guide to setting up an API. The docs for the quotable API can be found here.
Using the template tag is the most flexible way to get your API data and is preferred over the shortcode.
Please be aware that there are some limitations when using the shortcode. Here is a rundown of limitations:
- In the admin settings for your endpoint, you must set the output as ‘JSON (as string)’. You will get PHP warnings if you set this option to ‘PHP array data‘
- You cannot put the data into a variable for later use and manipulation as you can with the template tag
- If your API only returns one piece of data, such as the temperature in New York or the price of TSLA stock, then you may be fine to use the shortcode. But if it returns complex, multidimensional data then you will need to look at using the template function or the Pro Plugin to get nested data
Create a new page
If you are working on a live site, the safest way to test your API is to create a test page and then add the shortcode to the test page. Here are the steps to do that:
- In the WordPress admin, go to Pages > Add New and create a page named Test API
Add the shortcode
- Add the below shortcode anywhere in your new page
[wpgetapi_endpoint api_id='quotable' endpoint_id='random' debug='true']
In the above code, the debug option is set to true, so you will see a bunch of debugging data appear on the page. You can set the debug to false to remove this debugging info.
Shortcode attributes
The following shortcode attributes are available:
- api_id – (required) this is the ID of the API as set by you in the Setup page
- endpoint_id – (required) this is the ID of the endpoint as set by you in the API page
- debug – can be set to true or false and will display debug information if set to true
Extra shortcode attributes in the Pro Plugin
The Pro Plugin will add the following attributes to the shortcode:
- keys – allows you to target a specific key or index with nested, multidimensional data
- query_variables – set variables in the query string
- endpoint_variables – set variables within endpoints