Task: Create a "plugin" on WP, which will generate the form and display it on certain pages. The form, with the help of AJAX, will receive the necessary data from the database, depending on the previously filled items of this form.

Question: How to implement correctly?

If you create a wp_ajax_ (action) hook with a function in the main file of the plugin, make JS connections, then everything works.

It is impossible to make ajax, if all the code above, add to the main file with the help of include. Error 400 pops up

Code in the main plugin file:

include_once UrlPlugin.'addEditVote/AddEditVote.php'; 

The file AddEditVote.php (everything below is located in a separate folder):

 // при срабатывании хука действия wp_ajax с экшеном hello, выполняется функция say_hello add_action( 'wp_ajax_hello', 'say_hello' ); function say_hello() { echo "УРАААААААААААА!"; wp_die(); } // при хуке действии wp_enqueue_scripts, вызываем функцияю, которая зарегистрирует JS код обработчика AJAX и выставил её после jQuery add_action('wp_enqueue_scripts', function () { wp_enqueue_script('ajaxAddEditVote', plugins_url('ajaxAddEditVote.js', __FILE__), array('jquery'), null); // До вывода JS, вызванного wp_enqueue_script, выводим JS объект с какими либо значениями wp_localize_script('ajaxAddEditVote', 'localizePlugin', array( 'ajaxURL' => admin_url('admin-ajax.php') )); }); 

And the JS file ajaxAddEditVote.js:

 jQuery(document).ready( function() { var data = { action: 'hello' }; jQuery.post(localizePlugin.ajaxURL, data, function (response) { alert('Получено с сервера: ' + response); }); }); 

How to do it correctly (the sequence of connecting files and requests, read a bunch of topics, and as a result, complete confusion =). thank) ?

    1 answer 1

    In general, everything is done correctly, and this is confirmed by the work when the code is included in the main file of the plugin.

    The error in the removal in include is that you use the magic constant __FILE__ , which indicates the current code file. When removing the code __FILE__ gets a different value, and wp_enqueue_script() does not run your script at all.

    Still need to add nonce, according to the manual . Now, in theory, your website can be made to execute the server code say_hello() by sending a POST request from outside.

    UPDATE

    From the comments it is clear that you have taken the js script file to the same folder as the php code. In this case, the script will run, everything is correct. Error 400 is caused by the fact that you are missing a string.

     add_action( 'wp_ajax_nopriv_hello', 'say_hello' ); 

    When trying to execute an ajax request from a logged in user, the wp_ajax_nopriv_hello hook is wp_ajax_nopriv_hello . In the absence of such a hook, WordPress terminates with code 400. See line 101 in the wp-admin/admin-ajax.php .

    • Rechecked again, in the code of the page, js, connected with wp_enqueue_script, is present and wp_localize_script is located in front of it. Let the files be valid. Didn't quite understand exactly how to do it then? I will definitely study and do nonce, but as soon as I understand this question - Gagablik
    • And I can not answer as it should, without seeing the paths to your files. - KAGG Design
    • Completed the answer. - KAGG Design