WP custom api endpoint

Da chiamare dopo rest_api_init, si utilizza la funzione register_rest_route che accetta 3 argomenti:

  • namespace
  • route
  • argomenti

Il link finale sarà [url_sito]/wp-json/[namespace]/[route].

Codice

            
function lpuk_api_awesomelink_post( WP_REST_Request $request )
{
  $title = $request->get_param('title');
  $link = $request->get_param('link');
  if(empty($link)) {
    return 'Nessun link fornito';
  }
  return 'ciao ' . $title . ' ' . $link;
}

add_action('rest_api_init', function() {
  register_rest_route('lpuk_awesomelink/v1', '/new', array(
    'methods' => ['GET','POST'],
    'callback' => 'lpuk_api_awesomelink_post'
  ));
});          
Link Utili