[wp-trac] [WordPress Trac] #35983: Better support for "singular" endpoints

WordPress Trac noreply at wordpress.org
Sun Feb 28 04:47:13 UTC 2016


#35983: Better support for "singular" endpoints
---------------------------+-----------------------------
 Reporter:  dd32           |      Owner:
     Type:  enhancement    |     Status:  new
 Priority:  normal         |  Milestone:  Awaiting Review
Component:  Rewrite Rules  |    Version:
 Severity:  normal         |   Keywords:  has-patch
  Focuses:                 |
---------------------------+-----------------------------
 WP_Rewrite endpoints are currently geared towards endpoints that capture
 content after it, for example, `/feed/FEED_TYPE/`.

 Quite often we instead want to create an endpoint which is instead
 `/embed/` or `/faq/` we don't need to capture an optional bit of data
 after it.

 Currently endpoints can operate in either mode, if you specify and
 endpoint of `test-endpoint` then both `/endpoint-name/` and `/endpoint-
 name/data-passed/` will work.
 The problem comes when testing for the existence of that endpoint in the
 query.
 An endpoint which passes data is easy to check `if ( get_query_var
 ('endpoint-name') )`, an endpoint which doesn't - not so easy `global
 $wp_query; if ( isset( $wp_query->query_var['single-endpoint'] ) )`.

 Something that would be easier, and nicer for developers using the rewrite
 endpoint is to instead match the endpoint name itself:
 Currently the rewrite rule would look like this:
 {{{
 ([^/]+)/endpoint-name(/(.*))?/?$   => index.php?name=$1&endpoint-name=$3
 }}}

 What would be great, is if we can do instead:
 {{{
 add_endpoint( 'single-endpoint', ... );
 ([^/]+)/(single-endpoint)/?$  => index.php?name=$1&single-endpoint=$2
 }}}
 Which if used in conjunction with `add_endpoint()`'s 3rd parameter
 allowing us to specify a custom query_var:

 {{{
 add_endpoint( 'endpoint-name', ..., 'special_query' );
 add_endpoint( 'another-endpoint-name', ..., 'special_query' );

 Rules:
 ([^/]+)/(endpoint-name)/?$   =>  index.php?name=$1&special_query=$2
 ([^/]+)/(another-endpoint-name)/?$  => index.php?name=$1&special_query=$2
 }}}

 this would then allow us to use `if ( get_query_var( 'special_query' ) )`
 or `if ( get_query_var( 'single-endpoint' ) ) ` in the case of the first
 example.

 Attached is an implementation which upgrades the 3rd parameter
 (`$query_var`) to an array to support this. It's a super-rough patch, but
 appears to work. Does anyone else feel that this is worthwhile supporting?

--
Ticket URL: <https://core.trac.wordpress.org/ticket/35983>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list