This guide shows you how to retrieve URL parameters (also called query strings) in WordPress, such as ?utm_source=google or ?ref=friend. You’ll learn safe methods using WordPress built-in functions, how to display them in posts or pages, and how to avoid common security pitfalls. No coding experience is required beyond copying a few snippets.
What You’ll Need
- A WordPress site (self-hosted or managed) with admin access.
- Ability to edit theme files (via Appearance → Theme File Editor) or install a plugin.
- Basic understanding of PHP (if you choose the code method).
Step-by-Step
- Identify the parameter name. Look at the URL containing the parameter. For example, in
https://yoursite.com/?ref=friend, the parameter name isrefand its value isfriend. - Choose your method. You can use a plugin (no code) or a PHP snippet (more control). For a quick solution, install a plugin like “Insert PHP Code Snippet” or “Code Snippets”. For a permanent solution, edit your theme’s
functions.phpor a template file. - Using a plugin (recommended for beginners):
- Install and activate a code snippet plugin.
- Create a new snippet and paste the following code:
<?php $ref = isset($_GET['ref']) ? sanitize_text_field($_GET['ref']) : ''; echo $ref; ?>- Place the snippet where you want the value to appear (e.g., in a post via shortcode or in a template).
- Using a template file (for developers):
- Open the template file where you want to display the parameter (e.g.,
header.php,single.php, or a custom page template). - Add the following PHP code at the desired location:
<?php if (isset($_GET['ref'])) { echo 'Referrer: ' . esc_html($_GET['ref']); } ?>- Save the file. The parameter value will now appear on pages that use that template.
- Open the template file where you want to display the parameter (e.g.,
- Using a shortcode (for posts/pages):
- Add this to your theme’s
functions.phpor via a code snippet plugin: function get_url_param_shortcode($atts) { $atts = shortcode_atts(array('param' => ''), $atts); return isset($_GET[$atts['param']]) ? esc_html($_GET[$atts['param']]) : ''; } add_shortcode('url_param', 'get_url_param_shortcode');- In any post or page, use
[url_param param="ref"]to display the value of?ref=.
- Add this to your theme’s
- Test it. Visit a page with the parameter in the URL (e.g.,
?ref=test) and confirm the value appears.
Common Mistakes
- Not sanitizing input. Directly echoing
$_GET['param']without escaping can lead to XSS attacks. Always useesc_html()orsanitize_text_field(). - Checking wrong variable. Ensure you use
$_GET(not$_POST) for URL parameters. - Hardcoding the parameter name. If you change the parameter later, you must update every instance. Use a variable or a shortcode attribute.
- Placing code in the wrong file. Editing
functions.phpincorrectly can break your site. Always back up your files first.
Tips
- Use
esc_html()when outputting to the page to prevent HTML injection. - For multiple parameters, loop through
$_GETor use separate checks. - Consider using
get_query_var()if you’re working with WordPress rewrite rules, but for standard query strings,$_GETis fine. - Test with special characters (spaces, ampersands) by URL-encoding them (e.g.,
%20for space). - If you need the parameter in JavaScript, use
wp_localize_script()to pass it safely.
Troubleshooting
Parameter not showing: Check that the URL actually contains the parameter (look at the address bar). Ensure you’re using the correct parameter name (case-sensitive). If you’re using a shortcode, make sure the shortcode is registered correctly.
White screen after editing functions.php: This is usually a PHP syntax error. Immediately revert the file via FTP or your hosting file manager. Always test code on a staging site first.
Value appears but is empty: The parameter might be present but with no value (e.g., ?ref=). Use !empty() instead of isset() to check for a non-empty value.
Plugin conflict: If using a plugin, deactivate other plugins to see if one is interfering. Some caching plugins may also serve a cached page without the parameter.
Frequently Asked Questions
Can I get URL parameters in WordPress without coding?
Yes, use a plugin like “Query String Generator” or a shortcode plugin that provides a shortcode to output the value. However, most plugins require a small snippet to be truly flexible.
Is it safe to use $_GET in WordPress?
Yes, if you sanitize and escape the output. Never trust user input; always use functions like sanitize_text_field() and esc_html().
How do I get multiple URL parameters?
You can access each by name: $_GET['param1'], $_GET['param2'], etc. Or loop through $_GET to get all.
Can I get URL parameters in a WordPress page builder (Elementor, WPBakery)?
Yes, most page builders allow you to insert shortcodes or PHP snippets. Use the shortcode method above or a dynamic tag if your builder supports it.
Why is my parameter value showing as “Array”?
That happens when the URL has duplicate parameters like ?ref=a&ref=b. WordPress stores them as an array. Use $_GET['ref'][0] or end($_GET['ref']) to get a single value.
Can I use URL parameters in WordPress REST API?
Yes, the REST API accepts query parameters. You can access them in your custom endpoint using $request->get_param('param').
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Samsung Smart Speaker Rollable Display: Facts
- HP 9730e: Wide Prints, Not A3 Scans
- What 8888 Does When You Use It as DNS Server
- Credential Manager: Seamless Sign-In Explained
- API Governance for Public Data: What Growing Portfolios Need
- Enterprise AI Hidden Cost: Data Architecture ROI