1

Let say I've a custom page with slug created "/products", I want user will be able to POST to a form with url "/products/create" and allow us to render some JSON output.

The "/products/create" page does not actually exist.

I see from [1], the basic approach are

  1. Add a rewrite if match my target url, add a custom query var, e.g. is_my_action=1
  2. Register the custom query var (is_my_action) via "query_vars" action hook
  3. In the parse_request hook check for this query var, and perform my works.

This approach work, but seems a little hackish, are there any potential issue with the above approach?

Are there any better way to add custom virtual url that ?

[1] http://www.coderrr.com/create-an-api-endpoint-in-wordpress/

1 Answer 1

2

The approach is pretty much sound, this is how these things work in WordPress.

You might want to use add_rewrite_enpoint() for implementation though, since it compresses multiple steps into on integrated call and fits many use cases well.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.