If you aren’t familiar with PHP, it’s recommended to use the Shortcode.
The Basics
The WPGetAPI plugin creates a new function (or template tag), which at it’s simplest, looks like this:
wpgetapi_endpoint( 'myapi', 'myendpoint' );
Adding this template tag to one of your theme files (or by using a code snippet plugin) will call the API and the endpoint that you set within the first 2 parameters.
In the above example, it would call the API with the Unique ID of ‘myapi’ and the endpoint with the ID of ‘myendpoint’
Results Format
Because the template tag is PHP code, you will most likely want the Results Format set to PHP array data within the endpoint settings.
Using debug mode
The simplest way to test out the call to your endpoint is to copy the template tag from the top of your endpoint page and paste it wherever you need it in your theme file.
You should assign a variable to it, set the debug argument to true and then echo the variable, like shown below:
$data = wpgetapi_endpoint( 'quote', 'random', array( 'debug' => true ) );
echo $data;
This will output a bunch of debugging information to the page like so:
The Data Output section is what the API is actually returning to us, so if this looks good, we can then work with the data however we like.
Working with the data
Looking at the example above, if we simply want to output Robb Stark we would set debug to false and now echoing the ‘name’ key:
$data = wpgetapi_endpoint( 'quote', 'random', array( 'debug' => false ) );
echo $data['character']['name'];
To display the actual quote (the ‘sentence’ key) and the author, we would do something like this:
$data = wpgetapi_endpoint( 'quote', 'random', array( 'debug' => false ) );
echo $data['sentence'];
echo $data['character']['name'];
Going further with HTML
The above examples would not be formatted in a nice way, so we can mix in some HTML like shown below:
<?php
$data = wpgetapi_endpoint( 'quote', 'random', array( 'debug' => false ) );
?>
<div class="content"><?php echo $data['sentence']; ?></div>
<div class="author"><?php echo $data['character']['name']; ?></div>
<?php
This will then allow us to style the content and author classes with a little CSS.
We won’t go into a full-blown PHP/HTML/CSS tutorial here, but if you know a little bit of these, then you will be able to see how useful and easy it can be to style up your API content.
Extra Arguments
The PRO plugin adds extra arguments that are available to use alongside the debug argument:
$data = wpgetapi_endpoint( 'myapi', 'myendpoint',
array(
'debug' => false,
'endpoint_variables' => array(),
'query_variables' => 'param1=foo,param2=bar',
'body_variables' => array(),
'format' => 'html',
'html_tag' => 'li',
'html_labels' => false,
'html_url' => '',
'html_to_link' => '',
'img_key' => '',
'img_prepend' => '',
)
);
The most useful arguments when using the template tag will be the first 4 arguments shown: debug, endpoint_variables, query_variables and body_variables.