This Dynamic Shortcode is available from version 1.5.0
The Build URL Shortcode is a powerful tool for creating dynamic URLs within Dynamic Shortcodes. It allows you to construct complex URLs by combining a base URL with various query parameters.
This shortcode is particularly useful when working with APIs, creating custom links, or generating URLs with multiple parameters.
Syntax
{build-url:base_url@param1=value1 param2=value2 ...}
Key arguments
The Build URL Shortcode does not use positional arguments except for the base URL. All other parameters are provided as key arguments.
Examples
Basic URL construction
{build-url:'https://api.example.com'@endpoint=users}
This example constructs a URL by adding a query parameter to the base URL. The resulting URL would be:
https://api.example.com?endpoint=users
Multiple query parameters
{build-url:'https://search.example.com'@q=dynamic shortcodes&category=documentation}
This example adds multiple query parameters to the base URL. The resulting URL would be:
https://search.example.com?q=dynamic%20shortcodes&category=documentation
Using Dynamic Shortcodes for parameter values
{build-url:'https://api.example.com'@user_id={user:id} token={acf:api_token}}
This example uses other Dynamic Shortcodes to provide values for the query parameters. Assuming
{user:id}
resolves to "123" and{acf:api_token}
resolves to "abc123", the resulting URL would be:
https://api.example.com?user_id=123&token=abc123
Empty parameter value
{build-url:'https://api.example.com'@filter}
This example demonstrates how to add a parameter without a value. The resulting URL would be:
https://api.example.com?filter=
Combining with other Dynamic Shortcodes
{api:{build-url:'https://api.example.com'@endpoint=users&id={user:id}}}
This example shows how the Build URL Shortcode can be used within other Dynamic Shortcodes, such as the API shortcode.
It constructs the URL dynamically before making the API call.