Make WordPress Core

Opened 12 years ago

Closed 12 years ago

#21335 closed defect (bug) (invalid)

add_rewrite_endpoint() malfunction

Reported by: fike's profile Fike Owned by:
Milestone: Priority: normal
Severity: minor Version: 3.4.1
Component: Rewrite Rules Keywords:
Focuses: Cc:

Description

  1. If add_rewrite_endpoint() is called just right from plugin file, it will result in standard PHP error, telling that $wp_rewrite isn't initialized yet (call to blahblahblah on a non-object). I guess there should be placed _doing_it_wrong() function explaining that add_rewrite_point should be called during 'init' action (it would be even better if add_rewrite_endpoint() would automatically store passed arguments until 'init' action and would tell developer that his callings are late if add_rewrite_point() is called after uri parsing - as for me, finding the right action during core interaction is VERY complicated).
  2. add_rewrite_endpoint() doesn't work on my localhost at all (404 is being returned without any additional query vars; i've tried adding endpoint 'json' to EP_ALL, EP_PAGES, EP_PERMALINKS, etc. and calling site/page/json and site/page/json/x), even if i just copy example code and declare new query var through 'query_vars' filter. Tried this on customized theme and on fresh install with twenty-eleven theme with no success.

Change History (3)

#1 @SergeyBiryukov
12 years ago

Point 1 also applies to add_feed() (#4619, #20431) and the other Rewrite API functions, I suppose. There's a note in the Codex article:

Note that these rules are usually called inside the init hook.

I could not reproduce point 2. My steps:

  1. Copy the example from Codex into a function:
    function add_json_rewrite_endpoint() {
    	add_rewrite_endpoint( 'json', EP_PERMALINK | EP_PAGES );
    }
    add_action( 'init', 'add_json_rewrite_endpoint' );
    
  2. Flush rewrite rules by visiting Permalink Settings screen.
  3. A post with the endpoint (e.g. /2008/02/hello-world/json/) is properly displayed, without a 404 error.
Last edited 12 years ago by SergeyBiryukov (previous) (diff)

#2 follow-up: @Fike
12 years ago

Looks like point 2 is resolved - that's more codex problem than core problem.
Calling $wp_rewrite->flush_rules() after adding new endpoint did the job (saving new permalinks in settings didn't). Now i'm completely lost - why add_rewrite_endpoint() should be called during each request, while it is added once and forever after flush_rules()?

#3 in reply to: ↑ 2 @duck_
12 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Replying to Fike:

Calling $wp_rewrite->flush_rules() after adding new endpoint did the job (saving new permalinks in settings didn't). Now i'm completely lost - why add_rewrite_endpoint() should be called during each request, while it is added once and forever after flush_rules()?

Because if rules get flushed again at a later point (e.g. user visiting permalinks options page) then your endpoint will be lost as it wasn't registered on that page load.

Closing this ticket as invalid.

Note: See TracTickets for help on using tickets.