Request object for validate_callback

A quick search through the WP-API source revealed where validate_callback is being used: wp-api/lib/infrastructure/class-wp-rest-request.php $valid_check = call_user_func( $arg[‘validate_callback’], $param, $this, $key ); In this case, $this is an instance of WP_Rest_Request – so there’s one. Now, for sanitize_callback: $this->params[ $type ][ $key ] = call_user_func( $attributes[‘args’][ $key ][‘sanitize_callback’], $value, $this, $key ); Same file, there’s … Read more

How to create an endpoint without creating sub endpoints?

The rewrite rule added by: add_rewrite_endpoint( ‘my-endpoint’, EP_AUTHORS ); is author/([^/]+)/my-endpoint(/(.*))?/?$ => index.php?author_name=$matches[1]&my-endpoint=$matches[3] so it assumes anything after e.g. /author/henrywright/my-endpoint/…. You can try instead to replace it with add_rewrite_rule() e.g. add_rewrite_rule( ‘^author/([^/]+)/my-endpoint/?$’, ‘index.php?author_name=$matches[1]&my-endpoint=1’, ‘top’ );

Can I define multiple callback methods depending on the call method?

Check if your code looks like this because in the question you pass each method as separate function arguments (I have overlooked it earlier) add_action( ‘rest_api_init’, function () { register_rest_route(‘my-project/v1/’, ‘/form’, array( array(‘methods’ => ‘GET’, ‘callback’ => ‘GET_form’, ), array(‘methods’ => ‘POST’, ‘callback’ => ‘post_form’ ) ) ); }); As you can read in documentation: … Read more

How to have multiple rewrite endpoints in the same URL?

I also need this to work: example.com/nice-page/my-gallery/animals/cats/my-lightbox/1234 Where I’d like to register my-lightbox as an additional endpoint or query_var, because it could be used independently from without my-gallery, such as: example.com/another-page/my-lightbox/2345 Here’s an example of doing it using add_rewrite_rule(): add_action( ‘init’, function() { // This will let you use get_query_var( ‘my-gallery’ ). add_rewrite_endpoint( ‘my-gallery’, EP_PAGES … Read more

rewrite endpoint not working on home page

You’ll need to use a combination of add_rewrite_tag and add_rewrite_rule function setup_seo_endpoint() { // Ensures the $query_vars[‘item’] is available add_rewrite_tag( ‘%item%’, ‘([^&]+)’ ); // Requires flushing endpoints whenever the // front page is switched to a different page $page_on_front = get_option( ‘page_on_front’ ); // Match the front page and pass item value as a query … Read more

how to create endpoint for downloading pdf files?

A good action to hook in would be template_redirect. There you can query for your endpoint like this: add_action(‘template_redirect’, function() { global $wp_query; // Not your endpoint and not a page/post, we do nothing. if (!isset( $wp_query->query_vars[‘download’]) || ! is_singular()) { return; } // Here goes your magic! […] // And probably a good idea … Read more