0
votes

I have a plugin that uses WordPress wp rest api to to register a custom end point with register_rest_route() that invoke a callback that sends an HTML email to the logged in user, this HTML email has a link inside it that should hit back the rest api and should route to another custom route created with register_rest_route( ) function that should send the user age in the route params and again as a result a callback will be invoked and it should use the user age info and do something with it. I am able to send an email and view it in the inbox however when I press the link in the email(The link is something like this: http://127.0.0.1/myplugin/wp-json/mynamespace/v1/user/25) I don't hit the end point and get: {"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}} , on chrome debug console the error message is "Failed to load resource: the server responded with a status of 404 (Not Found) http://127.0.0.1/myplugin/wp-json/mynamespace/v1/user/25 "

//code for custom end points inside my plugin..

function my_register_endpoints(){


   register_rest_route( 
      // routes that sends an email is working
       .
       .
       .
   );

   register_rest_route(

      'mynamespace/v1',
      'user/(?P<age>[\d]+)',
      array(
          'methods' => 'POST',
          'callback' => 'my_callback',
          'args' => array(
                  'age' => array(
                      'required' => true,
                      'validate_callback' => function($param, $request, $key) {
                        return is_numeric( $param );
                       }
                  )
          ),
          'permission_callback' => function (WP_REST_Request $request) {

              if(!is_user_logged_in()){
                  return new WP_Error('login error',__('You are not logged in','My-Error'));
              }
              return true;

          }
      )

   );

 }

}

add_action('rest_api_init','my_register_endpoints');

function my_callback(WP_REST_Request $request){
  //should do something with $request['age']
  $age=$request['age'];
}

Will be glad if someone would be able to explain and give some examples about the process of hitting the wp rest api from a custom html email? How to do it? Can it be a regular link or should I use Ajax requests with plain JS / JQuery / Angular $http in my custom html email and invoke the call when hitting the link inside the email ?

( I am using mailcatcher to view the the emails, I am testing my wordpress site on XAMPP on localhost )

1

1 Answers

0
votes

Try using a custom page template rather than trying to call an endpoint from a link. That template can then call your endpoint if needed, either via AJAX or from the page template php directly.