Using the shortcode to display your API data is very easy and there are just 2 required attributes. There are also additional attributes available when using the Pro Plugin that can help to simplify getting your data.
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
Using the shortcode
Let’s look at how to use the shortcode in our real world example, using the Quotable API. You can see we have setup this API in our WordPress backend as shown below.
We can simply copy the ‘Shortcode’ from our endpoint and add this to a page or post and then view that page/post to see the result. The shortcode looks like the below:
[wpgetapi_endpoint api_id='quotable' endpoint_id='random' debug='false']
The debug option is set to false to not show debugging info. Changing false to true will output a bunch of debugging information on your page/post, which can be handy if you are running into issues with the API call.
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 – docs
- query_variables – set dynamic variables in the query string – docs
- endpoint_variables – set dynamic variables within endpoints – docs
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