Using the shortcode to display your API data is very easy and there are just 2 required attributes: api_id and endpoint_id.
[wpgetapi_endpoint api_id='your_api_id' endpoint_id='your_endpoint_id' debug='false']
In the example above, you would replace the ‘your_api_id’ and ‘your_endpoint_id’ with the actual id’s that you have setup in the admin area. You can then add the shortcode to any post, page, widget or other area that shortcodes are accepted.
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
Using the shortcode
Below is what the shortcode looks like pasted into a test page we have set up.You can see we have setup this API in our WordPress backend as shown below.
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 for troubleshooting if you are running into issues with the API call.
The data looks strange
The returned data might look something like the below. This is a JSON string and it would require formatting.
See our docs on how to format JSON data or how to format JSON to HTML.
Extra shortcode attributes in the Pro Plugin
The Pro Plugin adds the following extra attributes to the shortcode:
- keys – grab data from a specific key or index within nested, multidimensional array data – view docs
- query_variables – set dynamic variables in the query string – view docs
- endpoint_variables – set dynamic variables within endpoints – view docs
- format – can be set to html or number
- html_tag– can be set to div, li or span when using format=”html”
- html_labels – can be set to true or false on whether to display labels when using format=”html”
Shortcode limitations
Please note that there are limitations when using the shortcode.
- When using the shortcode, in the admin settings for your endpoint you must set the output as JSON string. You will get PHP warnings if you set this option to PHP array data
- 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