Make WordPress Core

Opened 5 months ago

Last modified 3 months ago

#51746 new feature request

Add a core wrapper function to retrieve $wp->request

Reported by: apedog Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords: reporter-feedback
Focuses: Cc:


Feature request: add a core wrapper function to retrieve global $wp->request.

Core should provide a function wp_get_uri_request() (or some other naming convention) to reliably get the uri request. And reduce reliance on globals/superglobals.

global $wp;
$url = $wp->request;

instead use:

$url = wp_get_uri_request();

Change History (5)

This ticket was mentioned in Slack in #core by chaion07. View the logs.

3 months ago

#2 @peterwilsoncc
3 months ago

  • Keywords reporter-feedback added

This was discussed in a triage session today. While the function you describe is simple enough it is a property mostly intended to be used internally by WordPress for the most part.

Are you able to provide a little more about the use cases in which you find yourself needing to get the $wp->request property? That will help decided whether or not the function and subsequent maintenance is necessary.

#3 @apedog
3 months ago

It's usage would be for plugins. Much the same way that home_url() is used.
I just ran a quick search on a local install and found four plugins that use this pattern $wp->request for pretty much the same functionality.
Each of these plugins had to involve an internal WordPress object, when they shouldn't have.

Two of them polyfilling a current_url() function like so:

function current_url(){
global $wp;
return home_url( $wp->request );

or a more involved solution:

global $wp;
return esc_url_raw(add_query_arg($wp->query_string, '', home_url($wp->request)));

The specific use-case I ran into was a toolbar item that was supposed to return to the same page they were called on:

global $wp;
'href' => add_query_arg( [ 'action' => 'some_action' ], $wp->request ),

This would read a lot cleaner (and would not require a global $wp in the method):

add_query_arg( [ 'action' => 'some_action' ], wp_request_uri() )

In much the same way as WordPress provides API like wp_remote_get(), wp_remote_request() to be used instead of file_get_contents or curl, an API for the current path should be available without having to include globals.

This is very much about not having to access the internal $wp object. But instead abstracting the request/query.
A current_url()/request_uri() (the naming of the function is not at issue here) should be as useful as home_url() for plugin developers. There is no need to know that the value comes from the $wp object.

This ticket was mentioned in Slack in #core by apedog. View the logs.

3 months ago

#5 @peterwilsoncc
3 months ago

Thanks for the reply, something along the lines of request_uri() makes sense as an API really helps clarify the purpose.

Note: See TracTickets for help on using tickets.