1

I would like to get the query parameters passed via the URL on a WordPress site. Very easy, I've never had a problem before.

According to the address bar and dev tools my request URL is:

https://example.com/register/early/?utm_source=Testing+MemberPress+Link&utm_campaign=xxxxxxxxxx-EMAIL_CAMPAIGN&utm_medium=email&utm_term=0_-xxxxxxxxxx-%5BLIST_EMAIL_ID%5D&mc_cid=xxxxxxxx

I have tried many ways to get the query parameters. The following code:

function get_query_params() {
    global $wp;
    
    $current_url = home_url( add_query_arg( array( $_GET ), $wp->request ) );
    $get_params = $_GET;
    $request_params = $_REQUEST;
    $query_var = get_query_var('mc_cid');
    
    echo '<pre>URL: ' . print_r( $current_url, true ) . '</pre>';
    echo '<pre>$_GET: ' . print_r( $get_params, true ) . '</pre>';
    echo '<pre>$_REQUEST: ' . print_r( $request_params, true ) . '</pre>';
    echo '<pre>query_var: ' . print_r( $query_var, true ) . '</pre>';
    
    die;
}
add_action( 'parse_request', 'get_query_params' );

function add_get_val() {
    global $wp;
    $wp->add_query_var('mc_cid');
}
add_action('init','add_get_val', 1 );

Renders the following result

URL: https://example.com/register/early
$_GET: Array()
$_REQUEST: Array()
query_var: 

Unfortunately, I have been working on this for hours, and I am unsure what else to try. I've done this many times and have never had a problem. What am I missing? Can plugins block this.

I did check the .htaccess file and nothing has been added.

4
  • This page is rendered by the plugin MemberPress. That might be the culprit, as this code works when that plugin is disabled or on other WordPress installs. I still need a solution though. Commented Jan 17, 2024 at 16:57
  • it's likely that your current URL code is causing problems in addition to the issue you're currently facing. parse_request is also very very early Commented Jan 17, 2024 at 17:05
  • @TomJNowell parse_request is well after init. It's one of the last things before the headers are set. I don't think that's extremely early. codex.wordpress.org/Plugin_API/Action_Reference Commented Jan 17, 2024 at 17:21
  • $_GET shouldn't be empty, does this problem go away if you run this code without memberpress activated on a generic page/post? It's very likely then that the cause of this is somewhere else, $_GET is the proper way to access this, and if something is wiping it clean it's not because you're meant to use another method Commented Jan 17, 2024 at 17:51

2 Answers 2

3

There's been a misunderstanding of what query vars actually are and what they're used for.

The correct way to read a URL parameter is the same way as every other PHP application:

$mc_id = $_GET['mc_cid'];

This is because query vars are not URL parameters! WordPress doesn't have a special way to retrieve URL parameters, it works the same way as every other PHP application, $_GET and filter_input.

What If $_GET is blank?

If $_GET is blank then it's not because you're meant to use a special method to access the parameters, it's probably because:

  • The URL with the parameters is redirecting and not preserving those parameters in the new URL
  • A plugin or theme is wiping $_GET, either intentionally or accidentally
  • The server is misconfigured at the Apache/Nginx level and not passing those values to PHP ( unlikely as you'd notice other issues and could easily test this by doing a search /?s=...

The precise reason isn't something that can be figured out from just what's in your question right now.

Then What Are Query Vars?

Query variables instead are a part of rewrite rules and WP_Query. A query var is a parameter that was passed to the main post query aka the WP_Query that the main loop runs off of, that powers the conditionals such as is_single/etc and determines the main template.

So when you do this the s is a query variable:

$q = new WP_Query( [ 's' => 'search terms' ] );

The confusion happens because in ugly permalinks the main post loop that determines the template and posts etc takes its arguments from the URL parameters. This is also why rewrite rules are regex's that map pretty permalinks into ugly index.php?var=value type URLs.

Additionally, the method used to add a query var that you used is also incorrect. Instead you would use the query_vars filter to add it to the whitelist, you would never call functions on the $wp global!

So if you're meant to use $_GET/filter_input why do these functions exist?

  1. Sometimes you need to grab a value from the main query, e.g. get_query_var( 'meta_query' )
  2. When using custom rewrite rules to create pretty URLs, sometimes it's helpful to add a custom query variable, e.g. for virtual pages that don't have an associated post or perform an action. Then you can look for these and use specific values to trigger code, but you would never see them in the URL itself
2
  • I appreciate your response, but this is not the solution to my problem. I understand that WordPress query_vars are different from URL query parameters. I did a terrible job explaining that in my question and combined the two. If you notice in my code the $_GET and $_REQUEST arrays return as empty. Commented Jan 17, 2024 at 17:13
  • @MitchellBennett this is probably because you asked for the proper way to get the URL parameters and I answered to solve the question as it was written which is $_GET, but it sounds like something else in your plugins/theme that is not in your question is wiping $_GET, making it impossible to access the data you want. It sounds like you actually want the answer to "Why is $_GET and $_REQUEST blank?" Commented Jan 17, 2024 at 17:57
2

@tom-j-nowell was correct, the issue as described was not the issue at all.

The host of the website was scrubbing the URL so that it could still cache the page and then returning the parameters to the URL after the cached page was loaded.

https://wpengine.com/support/utm-gclid-variables-caching/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.