[wp-trac] [WordPress Trac] #51746: Add a core wrapper function to retrieve $wp->request

WordPress Trac noreply at wordpress.org
Tue Jan 26 14:11:38 UTC 2021


#51746: Add a core wrapper function to retrieve $wp->request
-------------------------------+------------------------------
 Reporter:  apedog             |       Owner:  (none)
     Type:  feature request    |      Status:  new
 Priority:  normal             |   Milestone:  Awaiting Review
Component:  General            |     Version:
 Severity:  normal             |  Resolution:
 Keywords:  reporter-feedback  |     Focuses:
-------------------------------+------------------------------

Comment (by apedog):

 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:
 {{{#!php
 <?php
 function current_url(){
 global $wp;
 return home_url( $wp->request );
 }
 }}}
 or a more involved solution:
 {{{#!php
 <?php
 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:
 {{{#!php
 <?php
 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):
 {{{#!php
 <?php
 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.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/51746#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list