keepcalculator

06 Numbers and Powers

Get URL Parameters in WordPress (Easy Guide)

Card No. 06·72504 · Updated · Standard formula, shown below

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

  1. Identify the parameter name. Look at the URL containing the parameter. For example, in https://yoursite.com/?ref=friend, the parameter name is ref and its value is friend.
  2. 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.php or a template file.
  3. Using a plugin (recommended for beginners):
    1. Install and activate a code snippet plugin.
    2. Create a new snippet and paste the following code:
    3. <?php $ref = isset($_GET['ref']) ? sanitize_text_field($_GET['ref']) : ''; echo $ref; ?>
    4. Place the snippet where you want the value to appear (e.g., in a post via shortcode or in a template).
  4. Using a template file (for developers):
    1. Open the template file where you want to display the parameter (e.g., header.php, single.php, or a custom page template).
    2. Add the following PHP code at the desired location:
    3. <?php if (isset($_GET['ref'])) { echo 'Referrer: ' . esc_html($_GET['ref']); } ?>
    4. Save the file. The parameter value will now appear on pages that use that template.
  5. Using a shortcode (for posts/pages):
    1. Add this to your theme’s functions.php or via a code snippet plugin:
    2. 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');
    3. In any post or page, use [url_param param="ref"] to display the value of ?ref=.
  6. 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 use esc_html() or sanitize_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.php incorrectly 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 $_GET or use separate checks.
  • Consider using get_query_var() if you’re working with WordPress rewrite rules, but for standard query strings, $_GET is fine.
  • Test with special characters (spaces, ampersands) by URL-encoding them (e.g., %20 for 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

The whole Numbers and Powers drawer →