a tutorial on WordPress and using $_GET to output URL parameters.

In this tutorial, I’ll show you how to pass and output a URL parameter in WordPress using $_GET. Read on to learn how in this quick and easy tutorial!

What is $_GET in WordPress?

In WordPress, $_GET is the PHP code used to assign a URL parameter to a variable inside of a .php file.

You’ve likely seen URL parameters before on other websites. They look like this:

Website URL with URL Parameters

From the URL above, can you guess what are URL parameters and what are the values of the URL parameters?

That’s right, user & age are both URL parameters, and james and 32 are the values for those URL parameters.

As WordPress is written using the PHP scripting language, we can access those URL paramters inside of PHP files by using the $_GET[‘URL_PARAMETER_NAME’] code.

Using $_GET

Let’s go through an example together of how we might assign the values of the examples above to variables in WordPress PHP files.

For example, let’s assume that we want to display the value of the user URL parameter on a page in WordPress.

Website URL with URL Parameters

To do that, we can use the following code inside of a PHP file:

<?php $username = $_GET['user']; ?> <h2> <?php echo $username ?> </h2>

Let’s walk through the example above.

First of all, we open up PHP tags in the PHP file as we’re going to write some PHP code to assign the value of the URL parameter to a new variable.

That new variable is named $username.

Secondly, we use the WordPress $_GET[‘user’]; PHP code to get and assign the value of the user URL parameter, which in our case is James.

Finally, we create a heading 2 HTML tag outside of the PHP code.

Inside of that HTML tag, we open up a final set of PHP tags, and then:

echo $username

In PHP, echo outputs the value assigned to the variable name. In our case we are echoing the value of the $username variable.

The result will look something like this:

WordPress $_GET inside of a php file to output the value of a URL parameter

For more WordPress tutorials, why not check out How to Add an HTML Description to a Category in WordPress?

🚀 Follow My Journey from 0 to 100,000 Monthly Pageviews!

Starting with this brand-new blog, I'm sharing everything I do to get from 0 pageviews to 100,000 monthly pageviews, every step of the way.
Zero spam, just great content. Unsubscribe at any time.

💬 Leave a comment

Your email address will not be published. Required fields are marked *

We will never share your email with anyone else.