WordPress.org

Make WordPress Core

Opened 6 years ago

Last modified 2 weeks 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:

Description

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 6 years ago.
34699.diff (1.4 KB) - added by flixos90 4 years ago.
34699-unit-tests.diff (1.2 KB) - added by andizer 3 years ago.
Unit tests

Download all attachments as: .zip

Change History (14)

#1 @juliobox
6 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.
Thanks

#2 follow-up: @joehoyle
6 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
5 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
5 years ago

Hi,

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.

example:

http://example.com/wp-includes/js/jquery/jquery.js?ver=4.4.2

would become:

http://dev.eelv.fr/wp-includes/js/jquery/jquery-4.4.2.js

for this, I would use something like that:

<?php

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.

Version 0, edited 5 years ago by bastho (next)

#5 @swissspidy
4 years ago

  • Keywords needs-patch added

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

@flixos90
4 years ago

#6 @flixos90
4 years 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 4 years ago by flixos90 (previous) (diff)

#7 @swissspidy
4 years 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
4 years 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.

@andizer
3 years ago

Unit tests

#9 @andizer
3 years ago

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

#10 @swissspidy
3 years ago

  • Milestone changed from Awaiting Review to Future Release

#11 @usernameunkowns
2 weeks ago

https://uaenewsagency.blogspot.com/
https://dubaivehicle.blogspot.com/
https://uaeultimate.blogspot.com/
https://uaeblogonline.blogspot.com/
https://fddddsfddcdc.blogspot.com/
https://projectorliteblog.blogspot.com/
https://pakeducationalist.blogspot.com/
https://lionsonlineforcesacademy.blogspot.com/
https://dubaitimesblog.blogspot.com/
https://edcationinpakistan.blogspot.com/
https://mywebsitestorenew.blogspot.com/
https://mywebsitestoreold.blogspot.com/
https://theguestpostcontent.blogspot.com/
https://thesupercontent.blogspot.com/
https://thesuperbloggers.blogspot.com/
https://thebloggerfundablog.blogspot.com/
https://therunnerblogger.blogspot.com/
https://Foodreviewbloggerblog.blogspot.com/
https://thenewbieblogger.blogspot.com/
https://eindianbloggershub.blogspot.com/
https://eamericansupperblogger.blogspot.com/
https://eperfectaustralianblogger.blogspot.com/
https://themostwantedbloggernow.blogspot.com/
https://howyoudothatbloggers.blogspot.com/
https://whywearebestbloggernow.blogspot.com/
https://WhereIamgoing.blogspot.com
https://eLetmedownslowly.blogspot.com/
https://RedditRefrencesblogger.blogspot.com/
https://digitalTeamworkblogger.blogspot.com/
https://theseoandwriteragency.blogspot.com/
https://Theclientproblemsolver.blogspot.com/
https://theworldnewblogger.blogspot.com/
https://thewonderofworldbloggers.blogspot.com/
https://theestatementblogger.blogspot.com/
https://emobilesandgamesblog.blogspot.com/
http://social.technet.microsoft.com/profile/fashioninstitute/
https://bit.ly/3chMZO9
https://cutt.ly/oncdnP2
https://t.ly/WqVN
http://social.technet.microsoft.com/profile/lawyersarbi/
https://t.ly/UwAb
https://cutt.ly/4ncdQiR
https://bit.ly/3wVNvcD
http://social.technet.microsoft.com/profile/zardosi/
https://cutt.ly/2ncdElQ
https://t.ly/N7X2
https://bit.ly/34QYPdW
http://social.technet.microsoft.com/profile/rajendralawyer/
https://cutt.ly/TncsDUr
https://bit.ly/3g6qdKp
http://social.technet.microsoft.com/profile/barassociation/
https://bit.ly/2Sc69OP
http://social.technet.microsoft.com/profile/consumerlawyer/
https://cutt.ly/FncdvT0
https://bit.ly/3cjXZuF
http://bbs.now.qq.com/home.php?mod=space&amp;uid=1021004/
http://bbs.now.qq.com/?1021004/
http://social.technet.microsoft.com/profile/Findtrademark/
https://cutt.ly/KncdzdW
https://bit.ly/2TIpsja
http://social.technet.microsoft.com/profile/gstlawyers/
https://cutt.ly/Hncdj1Q
https://bit.ly/34LPMer
http://social.technet.microsoft.com/profile/backlinkesysteml/
https://cutt.ly/HncdhyW
https://bit.ly/3wZDrzc
http://social.technet.microsoft.com/profile/backlinkesystemm/
https://cutt.ly/encdfp9
https://bit.ly/3z1P0Id
http://social.technet.microsoft.com/profile/backlinkesystemn/
https://cutt.ly/incda40
https://bit.ly/3vVlDp8
http://social.technet.microsoft.com/profile/backlinkesystemo/
https://cutt.ly/8ncdpSk
https://bit.ly/34K6lr1
http://social.technet.microsoft.com/profile/backlinkesystemj/
https://cutt.ly/ZncdoIr
https://bit.ly/2SXhe6i
https://social.technet.microsoft.com/profile/zardosi/
https://social.technet.microsoft.com/profile/backlinkesystemk/
https://social.technet.microsoft.com/profile/gstlawyers/
https://social.technet.microsoft.com/profile/backlinkesystemp/
https://social.technet.microsoft.com/profile/backlinkesystemp/
https://social.technet.microsoft.com/profile/registercompany/
https://social.technet.microsoft.com/profile/fashioninstitute/
https://social.technet.microsoft.com/profile/Findtrademark/
https://social.technet.microsoft.com/profile/squrefeet/
https://social.technet.microsoft.com/profile/backlinkesystemn/
https://social.technet.microsoft.com/profile/backlinkesysteml/
https://social.technet.microsoft.com/profile/backlinkesystemj/
https://social.technet.microsoft.com/profile/finaceconsult/
https://social.technet.microsoft.com/profile/divvorcelegal/
https://social.technet.microsoft.com/profile/backlinkesystemm/
https://social.technet.microsoft.com/profile/backlinkesystemi/
https://social.technet.microsoft.com/profile/backlinkesystemq/
https://social.technet.microsoft.com/profile/backlinkesystemr/
https://social.technet.microsoft.com/profile/drtlaw/
https://social.technet.microsoft.com/profile/publicforum/
https://social.technet.microsoft.com/profile/tamilsir/
https://social.technet.microsoft.com/profile/realestatelaw/
https://social.technet.microsoft.com/profile/ncltadvocate/
https://social.technet.microsoft.com/profile/BankLoan/
https://social.technet.microsoft.com/profile/PropertyLawyer/
https://social.technet.microsoft.com/profile/backlinkesystemo/
https://social.technet.microsoft.com/profile/fashioninstitute/
http://social.technet.microsoft.com/profile/backlinkesystemk/
https://cutt.ly/sncdiWH
https://bit.ly/3fPHAQT
http://social.technet.microsoft.com/profile/backlinkesystemp/
https://cutt.ly/pncdyHb
https://bit.ly/3ifrvp9
http://social.technet.microsoft.com/profile/lawyersch/
https://cutt.ly/bncdtYk
https://bit.ly/2THHCSb
http://social.technet.microsoft.com/profile/registercompany/
https://cutt.ly/hncdrAG
https://bit.ly/3fNXB9K
http://social.technet.microsoft.com/profile/squrefeet
https://cutt.ly/CncdqTC
https://bit.ly/3z56WS7
http://social.technet.microsoft.com/profile/finaceconsult/
https://cutt.ly/mncs51e
https://bit.ly/3g8vUrg
http://social.technet.microsoft.com/profile/divvorcelegal/
https://cutt.ly/0ncs7De
https://bit.ly/2SVtp3J
http://social.technet.microsoft.com/profile/backlinkesystemi/
https://cutt.ly/Cncs4G4
https://bit.ly/3fMq3Jc
http://social.technet.microsoft.com/profile/backlinkesystemq/
https://cutt.ly/Sncs8lL
https://bit.ly/2SYKYQo
https://www.liveinternet.ru/users/caisar987522/profile
https://cutt.ly/Lncs976
https://bit.ly/2SXi82G
http://social.technet.microsoft.com/profile/backlinkesystemr/
https://cutt.ly/hncs2To
https://bit.ly/3phAtni
http://social.technet.microsoft.com/profile/drtlaw/
https://cutt.ly/Oncs0ta
https://bit.ly/2TxUk5K
http://social.technet.microsoft.com/profile/publicforum/
https://cutt.ly/YncsMBR
https://bit.ly/3gdj8YF
http://social.technet.microsoft.com/profile/realestatelaw/
https://cutt.ly/fncsBWy
https://bit.ly/3fRRLnG
http://social.technet.microsoft.com/profile/tamilsir/
https://cutt.ly/PncsVtk
https://bit.ly/3vSQsdN
http://social.technet.microsoft.com/profile/ncltadvocate/
https://cutt.ly/yncsXFH
https://bit.ly/3vWqBBS
http://social.technet.microsoft.com/profile/PropertyLawyer/
https://cutt.ly/oncsL88
https://bit.ly/34KlhFV
http://social.technet.microsoft.com/profile/BankLoan/
https://cutt.ly/jncsKW2
https://bit.ly/3gcT4gb
https://uaenewsagency.blogspot.com/
https://bit.ly/3yJwJ22
https://dubaivehicle.blogspot.com/
https://bit.ly/3yINs5M
https://uaeultimate.blogspot.com/
https://bit.ly/3vw08uz
https://uaeblogonline.blogspot.com/
https://bit.ly/3wweWJY
https://fddddsfddcdc.blogspot.com/
https://bit.ly/2TfRYIy
https://projectorliteblog.blogspot.com/
https://bit.ly/3fUyQrc
https://pakeducationalist.blogspot.com/
https://bit.ly/3wBIRQU
https://lionsonlineforcesacademy.blogspot.com/
https://bit.ly/3i0RUH4
https://dubaitimesblog.blogspot.com/
https://bit.ly/3fOoeKB
https://edcationinpakistan.blogspot.com/
https://bit.ly/3vw9yXd
https://mywebsitestorenew.blogspot.com/
https://bit.ly/34nhY75
https://mywebsitestoreold.blogspot.com/
https://bit.ly/3fqZ8lR
https://theguestpostcontent.blogspot.com/
https://nodepositpokeronline.com/
https://edex.adobe.com/member/H0VBng_md
https://bit.ly/3hZB6jC
https://adobe.ly/3fxWURN
https://onlinecasinoku.com/
https://edex.adobe.com/member/m668LxZKy
https://adobe.ly/3c4PlQp
https://bit.ly/2TkwbiZ
https://redbettips.com/
https://edex.adobe.com/member/r5FTQM4Y9
https://adobe.ly/3fvS7QQ
https://bit.ly/2SCyU6V
https://sharkcasinogames.com/
https://edex.adobe.com/member/gEEJupeb3
https://adobe.ly/3c3druY
https://bit.ly/2Te9TiP
https://redwinecasino.com/
https://edex.adobe.com/member/Pill23DNX
https://adobe.ly/34rDVSC
https://bit.ly/3c04vXc
https://publicforum.in/
https://edex.adobe.com/member/kUuSuKqZO
https://adobe.ly/3i1veGy
https://bit.ly/3oVl4ZI
https://lawyerschennai.com/
https://edex.adobe.com/member/_gZSmBWL2
https://adobe.ly/3fvOB9e
https://bit.ly/2RIlM0h
https://wwwtamilsiragugal.com/
https://edex.adobe.com/member/TW44VSdgu
https://adobe.ly/34ry7st
https://bit.ly/2RK348A
https://ncltadvocates.com/
https://edex.adobe.com/member/avVnuvrJh
https://adobe.ly/3i2SwMd
https://bit.ly/2RIlNkR
https://drtlaw.in/
https://edex.adobe.com/member/4mLkt0zfW
https://adobe.ly/3yQNcS7
https://bit.ly/3vqhj0G
https://realestatelaw.in/
https://edex.adobe.com/member/jyeQssh0l
https://adobe.ly/2SyZqOJ
https://bit.ly/2Sy2ElI
https://propertylawyer.org.in/
https://edex.adobe.com/member/Rr0eM5mMO
https://adobe.ly/2TtqLT1
https://bit.ly/2ToSm7P
https://bankloan.org.in/
https://edex.adobe.com/member/Tn9F_WPGt
https://adobe.ly/2RMXLVV
https://bit.ly/3bWei0f
https://consumerlawyer.in/
https://edex.adobe.com/member/mWcQ_DOw_
https://adobe.ly/3yWqRTx
https://bit.ly/2RJmp9X
https://findtrademark.org/
https://edex.adobe.com/member/w3RIxeJ1X
https://adobe.ly/3p0nm9Z
https://bit.ly/2Sulie5
https://gstlawyers.in/
https://edex.adobe.com/member/CWQu1Jp5k
https://adobe.ly/3fuxFjs
https://bit.ly/3yMuevS
https://buddypress.trac.wordpress.org/ticket/3755
https://bit.ly/3p6KKCw
https://cutt.ly/DngZR8y
https://shorturl.at/npxO2
https://https://tinyurl.com/4mzz9xr2
https://tiny.cc/7qcytz
https://shorturl.at/sLMY5
http://lnkiy.in/UFnsd
https://pxlme.me/bdZoNvXk
https://redirect.is/gtdy3ax
https://t.ly/mFky
http://bitly.ws/dGnv
https://n9.cl/bitlyyy
https://fashioninstitute.co.in/
https://edex.adobe.com/member/iz9iLOmJ0
https://adobe.ly/2SFDiSG
https://bit.ly/3vvb6kc
https://lawyersarbitrators.com/
https://edex.adobe.com/member/HzwB02DKQ
https://adobe.ly/3p4AU4o
https://bit.ly/3wwg7ZU
https://zardosi.in/
https://edex.adobe.com/member/s5lSTFiyP
https://adobe.ly/3p2VKkj
https://bit.ly/3vAzMYI
https://rajendralawoffice.com/
https://adobe.ly/2RMXIJJ
https://edex.adobe.com/member/Snf6tuTsn
https://bit.ly/3bZReOa
https://barassociation.in/
https://edex.adobe.com/member/Sv4VjZ_A3
https://adobe.ly/2SDk68r
https://bit.ly/3oZoBpC
https://registeracompany.in/
https://edex.adobe.com/member/KJDWKng9v
https://adobe.ly/3fvFk0H
https://bit.ly/3yLjcqH
https://www.bestsquarefeet.com/
https://edex.adobe.com/member/pQTa07ing
https://adobe.ly/3fU4Trn
https://bit.ly/34wvKEt
https://financeconsultants.net/
https://edex.adobe.com/member/TrbX55Kbk
https://adobe.ly/3i8OjHb
https://bit.ly/3bYCKhR
https://divorcelegal.in/
https://edex.adobe.com/member/lUWaOzfPT
https://adobe.ly/3uyYsPJ
https://bit.ly/3wwguUi
https://bit.ly/3vtRD3b
https://thesupercontent.blogspot.com/
https://bit.ly/3vwQVlP
https://thesuperbloggers.blogspot.com/
https://bit.ly/3yHXiVx
https://thebloggerfundablog.blogspot.com/
https://bit.ly/3vw9MO3
https://therunnerblogger.blogspot.com/
https://bit.ly/3uro9Sb
https://Foodreviewbloggerblog.blogspot.com/
https://bit.ly/3i0L8kK
https://thenewbieblogger.blogspot.com/
https://bit.ly/3yKy7S7
https://eindianbloggershub.blogspot.com/
https://bit.ly/3i0Lo3c
https://eamericansupperblogger.blogspot.com/
https://bit.ly/3wwfgZc
https://eperfectaustralianblogger.blogspot.com/
https://bit.ly/3yLihXh
https://themostwantedbloggernow.blogspot.com/
https://bit.ly/3yMyfQW
https://howyoudothatbloggers.blogspot.com/
https://bit.ly/3oWbp4U
https://whywearebestbloggernow.blogspot.com/
https://bit.ly/3wFPRfw
https://WhereIamgoing.blogspot.com/
https://bit.ly/3p9z02t
https://eLetmedownslowly.blogspot.com/
https://bit.ly/3yK1VhO
https://RedditRefrencesblogger.blogspot.com/
https://bit.ly/3uCT9z3
https://digitalTeamworkblogger.blogspot.com/
https://bit.ly/3yJXKCM
https://theseoandwriteragency.blogspot.com/
https://bit.ly/3vyet9Z
https://Theclientproblemsolver.blogspot.com/
https://bit.ly/2Tqot76
https://theworldnewblogger.blogspot.com/
https://bit.ly/3bZ2GK9
https://thewonderofworldbloggers.blogspot.com/
https://bit.ly/3wAJZEv
https://theestatementblogger.blogspot.com/
https://bit.ly/2RLaybo
https://emobilesandgamesblog.blogspot.com/
https://bit.ly/3wKnixP

Note: See TracTickets for help on using tickets.