Why is add_rewrite_endpoint incompatible with /tegory%/%postname%/ permalink structure?

I’ve added a myvar/myvalue rewrite endpoint to EP_ALL which can be added to any URL of a WP installation and a WP plugin will act upon it. That part is cool but it doesn’t work when the permalink structure is /%category%/%postname%/. It works well with all the built in, selectable structures. There could be other structures it doesn’t work with.

Doesn’t work = 404 error.

Read More

The path looks something like this: /mycategory/mypost/myvar/myvalue

I got the Rewrite analyzer plugin which tells me that it’s interpreted as (top 4, in order):


This is not it… (highlighted with yellow)

(.?.+?)/myvar(/(.*))?/?$
pagename: mycategory/mypost
myvar: myvalue


Apparently this gets used but it’s still cold…

.?.+?/([^/]+)/myvar(/(.*))?/?$
attachment: mypost
myvar: /myvalue


Plain wrong…

(.?.+?)(/[0-9]+)?/?$
pagename: mycategory/mypost/myvar
page: /myvalue


This is it, but it fails to realize it!

(.+?)/([^/]+)/myvar(/(.*))?/?$
category_name: mycategory
name: mypost
myvar: myvalue


And I’m outputting global $query_string; which tells me it’s
attachment=mypost&myvar=%2Fmyvalue (by the way it shouldn’t include the %2f – slash).

So can someone enlighten me if this is a bug in WP, or is there something I can do about it?

My idea is roughly this
if ( '/%category%/%postname%/' == $wp_rewrite->permalink_structure ){ ... }
and manually telling WP the category and the name and displaying that content. But I don’t know what goes as ... and what should I hook this into.

Related posts

1 comment

  1. Why doesn’t it work? I’m not sure. That particular pattern is the same as a parent/child page request, WordPress has to do some extra queries to determine if a post exists by that name, or if it’s a page. Why this breaks when you add an endpoint, I haven’t delved into the code to determine.

    How can you make it work? It appears that if you don’t use the EP_ALL mask and instead use the OR operator with all the individual masks, it will build the rules in such a way where those endpoints will correctly resolve.

    For example:

    function wpa_myvar_endpoint(){
        add_rewrite_endpoint( 'myvar', EP_PERMALINK | EP_PAGES );
    }
    add_action( 'init', 'wpa_myvar_endpoint' );
    

Comments are closed.