[wp-hackers] Possible bug with endpoints

Jennifer Hodgdon yahgrp at poplarware.com
Tue May 22 23:26:08 GMT 2007

My understanding of the query_vars and request filters (see 
wp-includes/classes.php for the code) is that

a) You use the query_vars to modify the list of variables WP knows 
about. Looks like your plugin function would do that correctly.

b) Then WP goes through this list of variables, to see if they are set 
by either POST, GET, permalink parsing, or some other manner, storing 
their values in an array of variable values. However, I agree with 
what you are saying: if the endpoint has no value after it, I think it 
doesn't get stored because the test used is 
!empty($perma_query_vars[$wpvar]) and '' and 0 are both "empty" 
according to the PHP doc on php.net.

c) This resulting array of values is passed to the request filter, for 
you to modify. I think the plugin function you wrote for query_vars 
will not work for that filter, because the result needs to be an 
associative array of endpoint => value.

Anyway, I think maybe you are right that the endpoint code ought to be 
modified for it to work for you, but I am not sure what else it might 

By the way, another possibility is to do something in the init that 
checks for your "endpoint" by parsing the URL directly, and then set 
some kind of global variable. It may be easier than trying to coerce 
the query vars and rewrite code to do what you want.

Another possibility is to define permalinks directly instead of using 
endpoints, which gives you more flexibility on what is stored in the 
variable array (for instance, you can set a value to 1). You use the 
generate_rewrite_rules action for that.  Of course, you have to add a 
lot of rules to simulate an endpoint.

Good luck,

Seamus Leahy wrote:
> Firstly, thank you Jennifer for your insight. So I have been doing lots of
> searching and testing on endpoints. I have written what I have come to
> understand about endpoints (please correct me if I am wrong at any point), a
> proposal/question about a change to make it more useful and intuitive to
> plugin developers, and the tests (seems to be a '0' bug) and plugin.
>  add_rewrite_endpoint($name, $place)
> Adds a new endpoint. An endpoint allows for an additional piece at the end
> of WordPress pretty URLs.  For example, the URL for a date archive is
> "example.com/YYYY/[MM/[DD/]]" and adding an endpoint of "displayformat"
> would create "example.com/YYYY/[MM/[DD/]][displayformat/<endpoint value>]".
> There are already several WordPress defined endpoints such as "trackback"
> and "paged". 
> The params:
>  string $name - Is the name of the endpoint and the text to be used in
> creating the rewrite rules.
>  number $place - This is a bit flag which tells to add the endpoint to what
> types of pages (and there associated rewrite rules). There are constants for
> the bit flags for the particular places: EP_PERMALINK, EP_ATTACHMENT,
> To retrieve the value of endpoint, use the function get_query_var($name)
> where $name is the name of the endpoint. This will return the value after
> the endpoint: "<URL>/endpoint/<the returned value for the endpoint>".
> ----------------------
> Proposal/Question about a change
> Currently the value returned back for an endpoint is what follows the
> endpoint. I understand where this can be useful for multiple values. But
> what about when the endpoint is a simple binary value. In which case, it is
> more likely that nothing will follow the endpoint in the URL:
> "example.com/2007/03/10/hello-world/show-html-source/". If nothing follows
> the endpoint, then perhaps another value could be returned. Here are three
> possible suggestions:
>    1. The name of the endpoint
>    2. true
>    3. An optional parameter for add_rewrite_endpoint which the default is an
> empty string
> ----------------------
> Tests and Observations:
> * The filter 'query_vars' has no affect.
> * The filter 'request' adds "testendpoint" as [0] to query vars no matter if
> the endpoint is present or not.
> * get_query_var(<endpoint name>) returns what comes after the endpoint.
> * If the value after the endpoint is '0', then the return value is ''.

> The Revised plugin:
> <?php
> /*
> Plugin Name: Test Endpoint
> Plugin URI:
> Description: 
> Version: 1.0
> Author: Seamus Leahy
> */
> function TestEndpoint(){
>     add_action('init', 'TestEndpointInit');
>     add_action('template_redirect', 'TestEndpointRedirect');
>     add_filter('query_vars', 'TestEndpointQueryVarsFilter');
>     add_filter('request', 'TestEndpointQueryVarsFilter');
> }
> function TestEndpointInit(){
>     global $wp_rewrite;
>     $wp_rewrite->add_endpoint("testendpoint", EP_ALL);
>     $wp_rewrite->flush_rules();
> }
> function TestEndpointQueryVarsFilter($vars){
>     $vars[] = 'testendpoint';
>     return $vars; 
> }
> function TestEndpointParseQueryFilter($query){
>  // nothing done here
> }
> function TestEndpointRedirect(){
>     $my_var1 = get_query_var('testendpoint');
>     $my_var2 = get_query_var(0);
>     if (empty($my_var1) && empty($my_var2)){
>         return;
>     }
>     echo get_query_var('testendpoint')." and ".get_query_var(0)."<br/>";
>     die( '<h1>We hit the endpoint: testendpoint!</h1>');
> }
> TestEndpoint();
> ?>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers

Jennifer Hodgdon

Poplar ProductivityWare * www.poplarware.com
Web Databases/Scripts * Modeling/Analysis/Palm OS Software

More information about the wp-hackers mailing list