Make WordPress Core

Opened 8 years ago

Last modified 5 years ago

#35983 new enhancement

Better support for "singular" endpoints

Reported by: dd32's profile dd32 Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Rewrite Rules Keywords: has-patch
Focuses: Cc:


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' );

([^/]+)/(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?

Attachments (1)

35983.diff (11.9 KB) - added by dd32 8 years ago.

Download all attachments as: .zip

Change History (3)

8 years ago

#1 @tyxla
8 years ago

+1 - I've used such name-only endpoints several times and it will be very nice if they're easy to work with. Checking for global $wp_query; if ( isset( $wp_query->query_var['single-endpoint'] ) ) works, but is not very friendly.

#2 @dd32
8 years ago

Just a quick note - I wrote this patch after writing the ticket, so ticket suggestions/contents might not match patch behaviour 1-1, but the idea is the same.

The patch is also a little bigger as I chose to remove magic key=>value and numeric arrays with named arrays in the process for more readability.

Note: See TracTickets for help on using tickets.