How To Extract URL Parameters in WordPress

close up of code

At any given moment, new data is being created on your website. In fact, every click a user makes results in a new piece of information. The challenge lies in harnessing that data and gaining valuable input from it. 

That’s where URL parameters can come in handy. We’ll explain more about what these are later in this post. In a nutshell, however, parameters can be used to pass key information to your website. That includes important information about its users, such as what buttons they click on and when. 

In this article, we’ll provide an overview of URL parameters. This will include details about what they are, as well as how to extract them from your WordPress website both manually and using a plugin. Let’s get started! 

What Are URL Parameters?

First, let’s make sure the basics are covered. A Uniform Resource Locator (URL) is what’s entered into a browser address bar in order to visit a website (i.e., https://mywebsite.com). Just as its name suggests, it’s a uniform way to locate the resources for any website that’s hosted online. 

In order to extract URL parameters, it’s important to understand that you’ll be working with ‘query strings’. This is a part of a URL that is typically added on after the Top-Level Domain (TLD), such as .com or .org. Its purpose is to designate the value of specific parameters.

The parameter portion is usually pretty easy to spot, as it starts with a question mark:

http://www.website.com/hats?productid=9876

The above example shows a URL parameter made up of what’s called a key and value pair. These are separated by an equals sign. In this case, the key is “productid”, and the value is “9876”.

As we mentioned earlier, URL parameters are also sometimes referred to as query strings. The important thing to remember is that these strings and parameters are generated almost any time a user clicks on something. For that reason, developers tend to find fault with this function, as it is very easy to expose sensitive information through a URL parameter. 

While they do provide potentially helpful data points, URL parameters can also cause issues for Search Engine Optimization (SEO). For example, if you were to filter products in an online store by what they are, their sizes, and their prices, each unique URL would feature a different query string. 

While these strings would all be pointing to different content displays, search engines would see the resulting URLs as repeat paths to the same content. This can be problematic, unless you take some time to make an allowance for them in Google Search Console.    

With that being said, if you’re looking for a way to extract URL parameters, you can use a plugin to assist in the process. There are also ways to more securely use this feature. In the rest of this post, we’ll walk through how to set up a dedicated plugin, and how to manually experiment with parameters in WordPress. 

Getting URL Parameters by Using a Plugin

Thanks to WordPress’ open-source codebase, developers have been able to provide us with many useful tools for what are otherwise complex tasks. This can make it easy to extract the parameter data from your website’s URLs. Let’s walk through how the process works.

Step 1: Choose a Plugin

Unfortunately, there are not many URL parameter plugins that have been well-tested with the most updated version of WordPress. However, there are a few options with favorable ratings. One of the more widely-installed tools is the URL Params plugin

url parameters

This plugin enables you to use shortcodes to display specific content, depending on the parameters present in the URL. For example, you can display content based on what type of user has signed in, prefill form fields, or even greet visitors by name. 

Step 2: Insert the Shortcode You Want to Use

The URL Params plugin provides a shortcode that you can use on your pages and posts. They can be used as-is, or tweaked to fit your needs. A standard URL Params shortcode begins with the following:

[urlparam param="" /]

For example, you can create conditional shortcodes that will return specific text depending on what parameters are present. Let’s insert a conditional example into a WordPress page, in order to see what it looks like in action. 

We’ve chosen our Contact Us page, which contains a form. Our conditional shortcode is set up to return designated text based on whether or not certain parameters are present. We’ll first select the Shortcode block in the Block Editor, and paste in our shortcode: 

editing contact us page in wordpress

This shortcode will check to see if the URL parameter indicates that the user is logged in. If not, text will display inviting them to fill out the form: 

contact us page in wordpress

It’s important to note that while this plugin has plenty of five-star reviews and many positive comments, it has not been updated in some time. Also, there are risks associated with passing information to your website in this way. It’s recommended that you fully review the plugin documentation before using it, and test it out on a staging site if possible. 

Getting URL Parameters by Modifying Page Code

If you prefer a more hands-on approach to working with URL parameters, you can modify your page code directly. Thanks to WordPress core’s flexibility, there are different ways to access URL parameters. 

If you want to understand the process of extracting parameters from a URL, we recommend familiarizing yourself with the WP_Query class. This is where you’ll find functions you can use to either write your own plugin, or tweak existing PHP code. 

For example, get_query_var enables you to retrieve any parameters (variables) that are present in the WP_Query class. This means you can only use it to access and retrieve variables that are publicly available in the class. These are all listed in the WordPress Codex.

With that being said, if you have variables present because you’ve created custom URLs, they will not be available. However, you can work around that issue by hooking into the query_vars() filter. This will add your new variables to the $var array. 

Securing Your Query Strings

Since passing data to your site through the URL is a potential security risk, you might also want to explore the use of the sanitize_text_field() function in WordPress. This can help ensure that you’re not receiving nefarious code strings, which could jeopardize your website. 

Additionally, depending on your specific use case, you can use the wp_reset_query() function. This is useful in situations with multiple loops and queries. It resets the current query back to the initial loop – a way of ‘cleaning the slate’ before initializing another separate and distinct query. 

Get Everything You Need With WP Engine

Passing information to your website by extracting URL parameters can be useful, but you might require advanced developer resources in order to pull it off. Fortunately, we have you covered. Here at WP Engine, we love trying out new techniques and helping you do the same. 

While you’re here, don’t forget to check out our WordPress hosting plans for your next project. Join our growing community of developers, and benefit from expert support and professional hosting technology!

Get started.

Build faster, protect your brand, and grow your business with a WordPress platform built to power remarkable online experiences.