[wp-hackers] Possible bug with endpoints

Seamus Leahy leahy at au.org
Fri May 18 15:28:25 GMT 2007

I think I have found a bug with rewrite endpoints, but I would like for
someone with more experience and knowledge to check it out since I am a
newbie at hacking WordPress.

When a rewrite endpoint is found, the query_var value for it is set to an
empty string which happens to also be what wp_query->get() returns for a
non-existent query_var.

(/wp-include/query.php) WP_query->get():
 494      function get($query_var) {
 495          if (isset($this->query_vars[$query_var])) {
 496              return $this->query_vars[$query_var];
 497          }
 499          return '';
 500      }

This next snippet of code is what I think is setting the endpoint value to
an empty string:

(/wp-include/rewrite.php) inside the function generate_rewrite_rules():
//build up an array of endpoint regexes to append => queries to append
if ($endpoints) {
    $ep_query_append = array ();
    foreach ($this->endpoints as $endpoint) {
       //match everything after the endpoint name, but allow for [...]
       $epmatch = $endpoint[1] . '(/(.*))?/?$';
      //this will be appended on to the rest of the query for each dir
      $epquery = '&' . $endpoint[1] . '=';
      $ep_query_append[$epmatch] = array ( $endpoint[0], $epquery );

To get my tests to work, I changed the following line of:
    $epquery = '&' . $endpoint[1] . '=';
    $epquery = '&' . $endpoint[1] . '='.$endpoint[1];

I created a test plugin and I did the testing with WP 2.2.
The plugin:

Plugin Name: Test Endpoint
Plugin URI:
Version: 1.0
Author: Seamus Leahy
function TestEndpoint(){
    add_action('init', 'TestEndpointInit');
     add_action('template_redirect', 'TestEndpointRedirect');

function TestEndpointInit(){
    global $wp_rewrite;
    $wp_rewrite->add_endpoint("testendpoint", EP_ALL);

function TestEndpointRedirect(){
    $my_var = get_query_var('testendpoint');

    if (empty($my_var)){
    die( '<h1>We hit the endpoint: testendpoint!</h1>');




The test:
  1. Install the plugin and activate it
  2. Go to an existing page, append to the URL "/testendpoint/", and go to
the new URL
     - Expected results: " We hit the endpoint: testendpoint!"
     - Actually results: The original page without the endpoint
  3. Change the file /wp_include/rewrite.php as stated above
  4. Repeat step 2
      - Actually results:  " We hit the endpoint: testendpoint!"

leahy at au.org | x220

More information about the wp-hackers mailing list