It is necessary to do something in the plugin so that when clicking on a link of a certain type it is given to JSON where token is the code generated by the algorithm of the current day (this code knows the server and the sender)

method is the method pointer for the plugin.

The link should look like

 http://domain.ru/wp-api/token/method?postdata=data 

I found a partial answer here

But how this thing is read, what it is and how to modify it for me.

  add_rewrite_rule('^wp-api/pugs/?([0-9]+)?/?','index.php?__wp-api=1&pugs=$matches[1]','top'); 

I'm ready to try to do everything myself, but just explain how to make an entry point and so that it works.

  • one
    The problem in what? - tutankhamun
  • @tutankhamun is that very very lazy, probably :) - alenkins
  • “so that when clicking on a certain type of link is given” - you mean, probably, not a user’s transition, but a request from a certain script to this address, in response to which you would receive an answer to your request? - Stanislav Belichenko

2 answers 2

If I correctly guessed in the comment to your answer what you really want to do, then there are two options for solving this problem. The first is to develop your own API , as advised by your link, that is, the entry point for external request sources. But this is a rather cumbersome option that you hardly need.

The second is to simply accept some requests and send a response in response. In the previous answer, another user gave you a general working advice for such a decision, but, as for me, it would be more appropriate to proceed in the manner described below.

Mainly because specifically for asynchronous requests from plug-ins, and you probably have such a task, the functionality described below is suitable (it is in the Plugin API section), while the option proposed by another user is using the redirects functionality. which is completely wrong methodologically.

This functionality is tied to such a hook: wp_ajax_ (action) / wp_ajax_nopriv_ (action) . In short, it works out of the box for requests formed from the admin panel, and with a little magic applied it works for the front of the site. A detailed description of how to do this is here .

There are three key points here:

  1. Write a function that will make a POST request to the address admin-ajax.php (the address will be used, including for the front).
  2. Write a callback that will be used to catch your request and respond to it, which, in turn, hang up on this request using the mentioned wp_ajax_(my_action) hook (some of the brackets change to your name). If the request needs to be done not only from the admin panel, then hang up the same callback using the wp_ajax_nopriv_(action) hook.
  3. Add to the front js object myajax (the name is arbitrary), in which you will store the address of your endpoint, the name of the request, the key to check the request to belong to your site ( wp_nonce , token, but this is not about your token, but about the token specifically for this functional) and other variables you need at the front.

    Look at the endpoints in WordPress. Add your endpoint, for example json . And through the hook add_action( 'template_redirect', 'function' ); parse the result.

     <?php /** * Добавить endpoint */ function mihdan_init() { add_rewrite_endpoint( 'json', EP_ALL ); } add_action( 'init', 'mihdan_init' ); /** * Разобрать запрос и подсунуть JSON */ function mihdan_template_redirect() { global $wp_query; $json = $wp_query->get( 'json' ); if ( ! $json ) { return; } // Код для получения JSON // ..................... wp_send_json( $json ); } add_action( 'template_redirect', 'mihdan_template_redirect' ); ?> 
    • one
      Although the link can find the answer to the question, it is better to point out the most important thing here, and give the link as a source. If the page to which the link leads will be changed, the response link may become invalid. - From the queue of checks - cheops
    • Added a detailed answer, thanks for the clarification - mihdan