Make WordPress Core

Opened 2 years ago

Last modified 2 days ago

#34699 new enhancement

New function: `get_query_arg()`

Reported by: sebastian.pisula Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: General Keywords: reporter-feedback has-patch 2nd-opinion has-unit-tests
Focuses: Cc:


For example i have url $url = 'http://example.com/?param=1&param2=2&param3=3' and I want get param2 so I use function: get_query_arg('param2', $url);

Second argument: exists function add_query_arg and remove_query_arg

Attachments (3)

34699.patch (1.1 KB) - added by sebastian.pisula 2 years ago.
34699.diff (1.4 KB) - added by flixos90 13 months ago.
34699-unit-tests.diff (1.2 KB) - added by andizer 5 weeks ago.
Unit tests

Download all attachments as: .zip

Change History (13)

#1 @juliobox
2 years ago

Hello Sebastian

Can you provide us 2 real examples of usage that you actually need?
One without the query param and another with the param.

#2 follow-up: @joehoyle
2 years ago

IMP this is little easier than just using parse_url directly, so doesn't add much. -1

#3 in reply to: ↑ 2 @swissspidy
2 years ago

  • Keywords close reporter-feedback added

Replying to joehoyle:

IMO this is little easier than just using parse_url directly, so doesn't add much. -1

Yeah, using parse_url and then parse_str should do the trick. If there aren't any valid use cases for core I'd say -1 as well.

#4 @bastho
2 years ago


I just have an use case:

  • Query strings in static source URLs are not the best for performances
  • Version parameter is very useful to prevent from unwanted browser cache

Someone (me) could want to hook into script_loader_src in order to move the version from query string to path.



would become:


for this, I would use something like that:


add_filter( 'style_loader_src', 'remove_wp_ver_par', 9999 );
add_filter( 'script_loader_src', 'remove_wp_ver_par', 9999 );
function remove_wp_ver_par( $src ) {
    if ( null !== $ver = get_query_arg( 'ver', $src) ){
        $src = preg_replace( '/\.(js|css)$/', '-_'.$ver.'.$1', remove_query_arg( 'ver', $src ));
    return $src;

With the appropriate htaccess/nginx rules, it does the trick.

Actually, I could do it another way but this is the lighter way.

Last edited 2 years ago by bastho (previous) (diff)

#5 @swissspidy
13 months ago

  • Keywords needs-patch added

A patch would help show the legitimacy of such a function.

13 months ago

#6 @flixos90
13 months ago

  • Keywords has-patch needs-unit-tests 2nd-opinion added; close needs-patch removed

IMO it makes sense to introduce this function. While it is not that complex to do it manually at this point, a function like that would still allow for cleaner code and it also makes sense to introduce this function for parity. We have add_query_arg() and remove_query_arg(), so one could easily assume there is also get_query_arg().

I provided a new patch with 34699.diff, where $key is also supported to be an array, so it works in the same way like the other two functions. Open question: If $key is an array and one of its keys is not available as query argument, should the result array include the key with value NULL or should it just be left out? For isset() it wouldn't matter, but for array_key_exists() it would. Leaving it out would make the function code a bit simpler, but it would also mean there could be notices if a user simply tries to access a key that is not set.

This function would still need to unit tests, in case the decision is to introduce it. I'll gladly provide those after we have it figured out.

Last edited 13 months ago by flixos90 (previous) (diff)

#7 @swissspidy
12 months ago

I'm not really sure about supporting an array as $key. It makes it way more complex. Also, the function name is get_query_arg() after all. I understand the historical reasons, but I feel like a separate get_query_args() function or array support being something the developer has to implement themselves makes more sense. I doubt it's used often though.

#8 @nevis2us
12 months ago

I believe this function could be useful for core & plugin devs for the reasons stated above by flixos90. I think it should match add_query_arg() as close as possible. That is it should accept an array of keys and return FALSE not NULL if a key isn't in the query. If $key is an array and one of its keys is not available as query argument, the result array should include the key with value FALSE. Again to match add_query_arg() calling conventions. My 2p. Thank you.

5 weeks ago

Unit tests

#9 @andizer
3 weeks ago

  • Keywords has-unit-tests added; needs-unit-tests removed

#10 @swissspidy
2 days ago

  • Milestone changed from Awaiting Review to Future Release
Note: See TracTickets for help on using tickets.