Below, you’ll find a detailed guide on how to add the Connect to external APIs – WPGetAPI Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Connect to external APIs – WPGetAPI Plugin shortcode not to show or not to work correctly.
Before starting, here is an overview of the Connect to external APIs – WPGetAPI Plugin and the shortcodes it provides:
"Connect to External APIs – WPGetAPI is a comprehensive WordPress plugin that simplifies your website's interaction with various external APIs, enhancing functionality and user experience."
- [wpgetapi_endpoint]
WPGetAPI [wpgetapi_endpoint] Shortcode
The WPGetApi plugin shortcode is designed to fetch data from specified API endpoints and display it on your WordPress site. It accepts various parameters like ‘api_id’, ‘endpoint_id’, ‘debug’, ‘args’, and more. These parameters help customize the data retrieval process according to your needs. The shortcode also supports AJAX calls for asynchronous data loading, and chaining of multiple API calls for complex data fetching scenarios. It ensures the data is sanitized and formatted correctly, providing a safe and efficient way to integrate API data into your WordPress content.
Shortcode: [wpgetapi_endpoint]
Parameters
Here is a list of all possible wpgetapi_endpoint shortcode parameters and attributes:
api_id
– The unique identifier of the API.endpoint_id
– The unique identifier of the endpoint.debug
– A boolean value to enable or disable debugging.args
– Array of arguments for the API call.keys
– Array of keys to extract from the API response.endpoint_variables
– Array of variables specific to the endpoint.query_variables
– Array of variables used in the API query.body_variables
– Array of variables used in the API body.format
– The format of the output.html_tag
– The HTML tag to wrap the output in.html_labels
– Whether to include HTML labels in the output.html_url
– The URL to be used in the HTML output.html_to_link
– The HTML element to be used as a link.img_key
– The key to extract the image URL from the API response.img_prepend
– The URL to prepend to the image URL.img_link
– The URL to link the image to.on
– The event that triggers the API call.button_text
– The text to display on the button.hide_button
– Whether to hide the button or not.button_spinner
– The spinner to display while the button is clicked.button_id
– The unique identifier of the button.ajax_output
– The CSS selector of the element to display the AJAX output.chain
– The chain of API calls to execute.chain_delay
– The delay between chained API calls.
Examples and Usage
Basic example – Displaying API data by referencing the API ID and endpoint ID
[wpgetapi_endpoint api_id=1 endpoint_id=1 /]
Advanced examples
Displaying API data with debug mode enabled, and specifying the format, HTML tags and labels.
[wpgetapi_endpoint api_id=1 endpoint_id=1 debug=true format=json html_tag=div html_labels=true /]
Using the shortcode to display API data with specific keys and endpoint variables. The data will be retrieved based on the specified keys and variables.
[wpgetapi_endpoint api_id=1 endpoint_id=1 keys=key1,key2,key3 endpoint_variables=var1,var2,var3 /]
Triggering the API call via AJAX, with a custom button text and specifying the output element for the AJAX response.
[wpgetapi_endpoint api_id=1 endpoint_id=1 on=ajax button_text="Call API Now" ajax_output=".custom_ajax_output" /]
Using the shortcode to chain multiple API calls together, with a specified delay between each call.
[wpgetapi_endpoint api_id=1 endpoint_id=1 chain=api2,api3 chain_delay=5 /]
PHP Function Code
In case you have difficulties debugging what causing issues with [wpgetapi_endpoint]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'wpgetapi_endpoint', 'wpgetapi_endpoint_shortcode' );
Shortcode PHP function:
function wpgetapi_endpoint_shortcode( $atts = array() ) {
$a = shortcode_atts( array(
'api_id' => '',
'endpoint_id' => '',
'debug' => false,
'args' => array(),
'keys' => array(),
'endpoint_variables' => array(),
'query_variables' => array(),
'body_variables' => array(),
'format' => '',
'html_tag' => 'div',
'html_labels' => 'false',
'html_url' => '',
'html_to_link' => '',
'img_key' => '',
'img_prepend' => '',
'img_link' => '',
'on' => '',
'button_text' => '',
'hide_button' => '',
'button_spinner' => '',
'button_id' => '',
'ajax_output' => '.wpgetapi_ajax_output',
'chain' => '',
'chain_delay' => '0',
), $atts );
if( ! isset( $a['api_id'] ) || $a['api_id'] == '' )
return __( 'api_id shortcode attribute is not set.', 'wpgetapi' );
if( ! isset( $a['endpoint_id'] ) || $a['endpoint_id'] == '' )
return __( 'endpoint_id shortcode attribute is not set.', 'wpgetapi' );
// sort out our keys if using them
if( ! empty( $a['keys'] ) ) {
// Create our array of values for keys
// First, sanitize the data and remove white spaces
$no_whitespaces_keys = preg_replace( '/\s*,\s*/', ',', filter_var( $a['keys'], FILTER_SANITIZE_STRING ) );
$a['keys'] = explode( ',', $no_whitespaces_keys );
}
// sort out our endpoint_variables if using them
if( ! empty( $a['endpoint_variables'] ) ) {
// Create our array of values for endpoint_variables
// First, sanitize the data and remove white spaces
$no_whitespaces_vars = preg_replace( '/\s*,\s*/', ',', filter_var( $a['endpoint_variables'], FILTER_SANITIZE_STRING ) );
$a['endpoint_variables'] = explode( ',', $no_whitespaces_vars );
}
// add our shortcode args to the actual 'args' within the endpoint call
$a['args']['debug'] = $a['debug'];
$a['args']['endpoint_variables'] = $a['endpoint_variables'];
$a['args']['query_variables'] = $a['query_variables'];
$a['args']['body_variables'] = $a['body_variables'];
$a['args']['format'] = $a['format'];
$a['args']['html_tag'] = $a['html_tag'];
$a['args']['html_labels'] = $a['html_labels'];
$a['args']['html_url'] = $a['html_url'];
$a['args']['html_to_link'] = $a['html_to_link'];
$a['args']['img_key'] = $a['img_key'];
$a['args']['img_prepend'] = $a['img_prepend'];
$a['args']['img_link'] = $a['img_link'];
$a['args']['shortcode'] = true;
$a['args']['on'] = $a['on'];
$a['args']['button_text'] = $a['button_text'];
$a['args']['hide_button'] = $a['hide_button'];
$a['args']['button_spinner'] = $a['button_spinner'];
$a['args']['button_id'] = $a['button_id'];
$a['args']['ajax_output'] = $a['ajax_output'];
$a['args']['chain'] = $a['chain'];
$a['args']['chain_delay'] = $a['chain_delay'];
// if we are doing ajax
if( isset( $a['args']['on'] ) && $a['args']['on'] == 'ajax' && ! wp_doing_ajax() ) {
if( class_exists( 'WpGetApi_Extras_Extend' ) ) {
add_action( 'wp_footer', array( 'WpGetApi_Extras_Extend', 'ajax_function' ) );
$button_text = isset( $a['args']['button_text'] ) && $a['args']['button_text'] != '' ? $a['args']['button_text'] : 'Call API';
ob_start(); ?>
<div class="wpgetapi_ajax_wrap" data-id="<?php esc_html_e( $a['api_id'] ); ?>-<?php esc_html_e( $a['endpoint_id'] ); ?>">
<button
class="button btn btn-primary wpgetapi_ajax_request"
id="<?php esc_html_e( $a['button_id'] ); ?>"
data-api="<?php esc_html_e( $a['api_id'] ); ?>"
data-endpoint="<?php esc_html_e( $a['endpoint_id'] ); ?>"
data-args="<?php esc_html_e( json_encode( $a['args'] ) ); ?>"
data-keys="<?php esc_html_e( json_encode( $a['keys'] ) ); ?>"
data-post_id="<?php esc_html_e( get_the_ID() ); ?>">
<span class="button_text"><?php esc_html_e( $button_text ); ?></span>
</button>
<div class="wpgetapi_ajax_output"></div>
</div>
<?php
$result = ob_get_contents();
ob_end_clean();
} else {
$result = 'You need the <a href="https://wpgetapi.com/downloads/pro-plugin">PRO plugin</a> for this AJAX functionality.';
}
} else {
// if we are starting the chain
if( ! empty( $a['args']['chain'] ) ) {
$delay = absint( $a['args']['chain_delay'] );
// get our result from this chain
$result = wpgetapi_endpoint( $a['api_id'], $a['endpoint_id'], $a['args'], $a['keys'] );
if( $delay > 0 && ! is_admin() )
sleep( $delay );
// set the transient for this chain if not the last chain
if( $a['args']['chain'] !== 'end' )
set_transient( 'wpgetapi_chain_' . sanitize_text_field( $a['args']['chain'] ), $result, $delay + 10 );
} else {
// do our standard return
$result = wpgetapi_endpoint( $a['api_id'], $a['endpoint_id'], $a['args'], $a['keys'] );
}
}
if( is_array( $result ) ) {
$result = sprintf(
__( 'The <b>Results Format</b> for this endpoint is set to \'PHP array data\' - shortcodes are unable to output this type of data.<br>Please change the Results Format to \'JSON string\' in the endpoint settings page.<br>Alternatively, please see our tutorial on %1s.', 'wpgetapi' ),
'<a target="_blank" href="https://wpgetapi.com/docs/format-api-data-as-html/?utm_campaign=Shortcode JSON&utm_medium=Admin&utm_source=User">how to format your API data as HTML</a>.'
);
}
if( $a['args']['format'] !== 'no_display' )
return $result;
}
Code file location:
wpgetapi/wpgetapi/frontend/functions.php
Conclusion
Now that you’ve learned how to embed the Connect to external APIs – WPGetAPI Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply