Hello! There is file.php in the theme directory, how to make it so that it has access to the standard php functions such as the_title (); etc.

get_template_part (file.php) does not work as it should, it displays the contents where you do not need to display.

In file.php, a small script, if briefly it forms some specific output, of records depending on the id record transferred to it via jqery and after the result is transferred back in html format.

    1 answer 1

    What you want to do is wrong in terms of the Wordpress architecture. It is not necessary to create files that run separately from Wordpress .

    If you want to do something through an AJAX- request in Wordpress write a plugin. Register your handler for the wp_ajax_nopriv action in wp_ajax_nopriv . Inside this handler you will have access to the Wordpress API.

    It will turn out something like this:

     add_action('wp_ajax_nopriv_myfunc', 'my_functionality_handler'); function my_functionality_handler() { // Тут выполняете ваши задумки и возвращаете результат } 

    In order for the handler to be invoked, you need to send a GET or POST request to wp-admin/admin-ajax.php . For the considered example, the request must have an action variable with the value myfunc (your value may be different).

    Additional Information:

    Updated

    By the way, you can really place the AJAX action handler in the functions.php file in your theme directory.

    • Thank you very much for the comment. Tell me how to adapt my js function to remove a plugin which I understand will be in function.php the code $ ("button [data-shouldrequest]"). On ('click', function () {$ .post ("cart.php" , {order: $ (this) .data ("unique-id")}) .done (function (data) {$ ('# cart'). html (data);});}); By the way, by the way, I added the file I did it with the require_once function, I am very superficially familiar with js, and I want to do it right, so I will be happy with your hint, or a link with an example and description. Thanks again) - Alex
    • @Alex Add the action: 'myfunc' parameter to the second parameter of the $.post call and write /wp-admin/admin-ajax.php - tutankhamun instead of cart.php
    • Thank you very much for the right decision!) - Alex
    • What Tutankhamen wrote without a doubt is a classic approach. I note, however, that there is some drawback. The fact is that with this approach, with each call, the whole WordPress system (including all plugins) is loaded via ajax. This significantly increases the processing time. If fast interaction with the script is critical, it seems to me that it is worth putting it into a separate file that will immerse only the necessary parts of the VI. I would like to know what you (tutankhamun) think about it. - s976
    • @ s976 For easy access to WP, there is a SHORTINIT constant. You can use, as written in the article . However, you will need to understand that most of the "native" functions will not work either, you will have to drag from the sources or write your bikes. - tutankhamun