How to Display URL Parameters in WordPress Using Shortcodes
By: John Louwe
Published: November 28, 2024
In this tutorial, we’ll explore two simple yet powerful shortcodes that you can use to display the URL parameters of the current page in WordPress. Whether you want to show a raw query string or a nicely formatted list of URL parameters, this guide will help you implement both solutions seamlessly.
The Backstory
This solution was born out of a specific challenge I encountered while working on our sales page. We wanted to show a particular section of the page only when a user visited it with a certain URL parameter, such as during holiday promotions like Black Friday, Cyber Monday, or Thanksgiving. While JetEngine’s Dynamic Visibility feature provides a “Request Parameter” option in Elementor Builder, this functionality is not available when using Gutenberg—or in my case, Kadence. I needed a way to implement this dynamic behavior without relying on Elementor, and that’s how these shortcodes came to life.
Shortcode 1: Displaying URL Parameters as a Raw Query String
This shortcode outputs the URL parameters in their raw format, such as ?name=John&age=30. It’s perfect if you want to show the exact query string without additional formatting.
The Code:
function display_raw_url_parameters_shortcode() {
// Get the current URL parameters
$query_string = $_SERVER['QUERY_STRING'];
// If there are no parameters, return a message
if (empty($query_string)) {
return 'No URL parameters found.';
}
// Return the raw query string
return '?' . esc_html($query_string);
}
// Register the shortcode
add_shortcode('display_url_parameters_raw', 'display_raw_url_parameters_shortcode');
How It Works:
1. The $_SERVER[‘QUERY_STRING’] fetches the raw query string from the URL.
2. If no parameters are found, it displays a friendly message.
3. Otherwise, it outputs the query string, prefixed with a ? for consistency.
Usage:
Add the shortcode [ display_url_parameters_raw ] (without the spaces) to any page or post. If the URL is:
https://example.com/page?name=John&age=30
The output will be:
?name=John&age=30
Shortcode 2: Displaying URL Parameters as a List
If you prefer a more user-friendly display, this shortcode outputs the URL parameters as a formatted list, showing each key-value pair.
The Code:
function display_raw_url_parameters_shortcode() {
// Get the current URL parameters
$query_string = $_SERVER['QUERY_STRING'];
// If there are no parameters, return a message
if (empty($query_string)) {
return 'No URL parameters found.';
}
// Return the raw query string
return '?' . esc_html($query_string);
}
// Register the shortcode
add_shortcode('display_url_parameters_raw', 'display_raw_url_parameters_shortcode');
How It Works:
1. The $_SERVER[‘QUERY_STRING’] fetches the raw query string from the URL.
2. parse_url() extracts the query string from the URL.
3. parse_str() converts the query string into an associative array.
4. A formatted list is generated using ul and li and HTML tags.
Usage:
Add the shortcode [ display_url_parameters ] (without the spaces) to any page or post. If the URL is:
https://example.com/page?name=John&age=30
The output will be:
- name: John
- age: 30
Try It Out
Want to see it in action? Add a URL parameter to this very page, such as ?show=secretcontent. For example:
https://yourwebsite.com/this-blog-post?show=secretcontent
When you add this parameter, you’ll see a new section appear right here! This demonstrates how dynamic visibility can be achieved using URL parameters.
When to Use Each Shortcode
How to Add These Shortcodes to Your WordPress Site
Conclusion
With these two simple shortcodes, you can easily display URL parameters on your WordPress site. Whether you need raw data for debugging or a polished format for user interaction, these solutions are versatile and easy to implement.
Looking to Buy Property in Cebu, Philippines?
Recent Blogs
How to Display URL Parameters in WordPress Using Shortcodes
In this tutorial, we’ll explore two simple yet powerful shortcodes that you can use to display the URL parameters of the current page in WordPress. Whether you want to show a raw query string or a nicely formatted list of URL parameters, this guide will help you implement both solutions seamlessly. The Backstory This solution…
How to Create a Custom WordPress Shortcode to Display User Meta Fields
Learn how to create a custom WordPress shortcode to display user meta fields easily. Discover tips for safely adding code with WP Code Snippet, plus see why JetEngine is the best tool for managing custom fields on your site!
Enhance Your BuddyBoss Login Page with BuddyCTA Plugin
Hello there! I’m John Louwe, a WordPress developer and a membership expert with a passion for crafting exceptional online experiences. Today, I’m excited to introduce you to a game-changing WordPress plugin that’s going to revolutionize your BuddyBoss login pages – the BuddyCTA Plugin! Meet John Louwe: Your WordPress Guru Before we dive into the magic…