Skip to main content
API - Dynamic Shortcodes
Updated over a week ago

This Dynamic Shortcode is available from the version 1.5.0.

The API Dynamic Shortcode enables users to perform HTTP requests directly within WordPress content. By using this Dynamic Shortcode, users can fetch data from external APIs, send data to remote servers, or interact with other web services. The API Shortcode supports GET and POST methods, headers, and request bodies, allowing developers and content creators to access and display dynamic content.

The use of key arguments allows users to customize requests precisely, including setting timeouts and specifying request methods.

Syntax

The basic syntax for the API Shortcode is as follows:

{api:<url>@<keyarg_list>}
  • <url>: A string representing the target URL for the HTTP request. It can be an identifier, number, nested string, or quoted string.

  • @<keyarg_list>: A list of key arguments that define the request's behavior. These can include headers, body content, method type, and timeout settings.

Key Arguments

The API Dynamic Shortcode supports several key arguments that allow you to customize HTTP requests:

  • post: Specifies to use the POST HTTP method. The default is get.

    Example: {api:'https://www.myurl.com'@post}

  • headers: An array of headers to include in the request.

    Example: {api:'https://www.myurl.com'@headers={array:@Content-Type=" application/json" Authorization="Bearer token"}}

  • body: The request body is typically used with post methods.

    Example: {api:'https://www.myurl.com'@body={array:@key=value}}

  • timeout: An integer specifying the maximum time, in seconds, to wait for a response. The default is 30.

    Example: {api:'https://www.myurl.com'@timeout=10}

  • no-decode: by default the result is treated as JSON and decoded, if you want to avoid it because the API does not return JSON, you can use this keyarg.

    Example: {api:'https://www.myurl.com'@no-decode}

Examples

Here are five examples demonstrating the API Shortcode in action:

  1. Simple GET Request

    {api:"https://api.example.com/data"}

    Explanation: This example makes a simple GET request to the specified URL (https://api.example.com/data). The default method is GET; no additional headers or body content are provided.

  2. GET Request with Headers

    {api:"https://api.example.com/data"
    @headers={array:@Authorization="Bearer token"}
    }

    Explanation: This example demonstrates how to include a header in the request, specifically an authorization token. The request is made to https://api.example.com/data using the GET method.

  3. POST Request with JSON Body

    {api:"https://api.example.com/submit"
    @post
    body={array:@name='John Doe' email='john.doe@example.com'}
    headers={array:"Content-Type: application/json"}
    }

    Explanation: This example sends a POST request to https://api.example.com/submit with a JSON payload containing name and email. The Content-Type header is set to application/json.

  4. GET Request with Custom Timeout

    {api:"https://api.example.com/data"@timeout=5}

    Explanation: This example performs a GET request with a custom timeout of 5 seconds to https://api.example.com/data. The default timeout is overridden to be more responsive.

Special Privileges

The API Shortcode must be executed within Power Shortcodes or in a privileged context.

Did this answer your question?