From f1f04c4f12d32563bd0bcebd2a6f8cba3119090d Mon Sep 17 00:00:00 2001 From: asus Date: Sat, 10 Feb 2024 02:22:16 +0100 Subject: [PATCH] wip found how to theorically get ajax request in php but for the moment i get error 400 --- plugins/wp_model_plugin/html/menu/menu.html | 2 ++ plugins/wp_model_plugin/js/menu/menu.js | 18 +++++++++++ plugins/wp_model_plugin/php/menu/menu.php | 32 ++++++++----------- .../php/utils/add_to_front.php | 3 -- .../wp_model_plugin/php/utils/create_html.php | 5 +-- plugins/wp_model_plugin/plugin_hooks.php | 8 +++-- 6 files changed, 41 insertions(+), 27 deletions(-) create mode 100644 plugins/wp_model_plugin/html/menu/menu.html create mode 100644 plugins/wp_model_plugin/js/menu/menu.js diff --git a/plugins/wp_model_plugin/html/menu/menu.html b/plugins/wp_model_plugin/html/menu/menu.html new file mode 100644 index 0000000..7ca76e8 --- /dev/null +++ b/plugins/wp_model_plugin/html/menu/menu.html @@ -0,0 +1,2 @@ + + diff --git a/plugins/wp_model_plugin/js/menu/menu.js b/plugins/wp_model_plugin/js/menu/menu.js new file mode 100644 index 0000000..2e65b1c --- /dev/null +++ b/plugins/wp_model_plugin/js/menu/menu.js @@ -0,0 +1,18 @@ +const inputElement = document.getElementById('mytext'); +const sendButton = document.getElementById('mybutton'); + +sendButton.addEventListener('click', () => { + const inputValue = inputElement.value; + const myurl = php_data.ajax_url; + console.log(myurl); + + fetch(myurl, { + method: 'POST', + credentials: 'same-origin', + body: JSON.stringify({ + action: 'get_data', + _ajax_nonce: php_data.nonce, + data: inputValue, + }) + }) +}); diff --git a/plugins/wp_model_plugin/php/menu/menu.php b/plugins/wp_model_plugin/php/menu/menu.php index c28aab3..f29c8bb 100644 --- a/plugins/wp_model_plugin/php/menu/menu.php +++ b/plugins/wp_model_plugin/php/menu/menu.php @@ -1,27 +1,21 @@ - + add_files_to_front( array( + "menu/menu.js", + )); - - "; + echo create_html("menu/menu.html"); } ?> diff --git a/plugins/wp_model_plugin/php/utils/add_to_front.php b/plugins/wp_model_plugin/php/utils/add_to_front.php index 14d79e8..93149ba 100644 --- a/plugins/wp_model_plugin/php/utils/add_to_front.php +++ b/plugins/wp_model_plugin/php/utils/add_to_front.php @@ -55,9 +55,6 @@ function add_var_to_front($vars, $handle = "init") { $handle = pathinfo($handle, PATHINFO_FILENAME); $object_name = "php_data"; - $ajax_url = admin_url( 'admin-ajax.php' ); - $vars["ajax_url"] = $ajax_url; - wp_localize_script($handle, $object_name, $vars); } diff --git a/plugins/wp_model_plugin/php/utils/create_html.php b/plugins/wp_model_plugin/php/utils/create_html.php index 8ce486a..235ddef 100644 --- a/plugins/wp_model_plugin/php/utils/create_html.php +++ b/plugins/wp_model_plugin/php/utils/create_html.php @@ -21,10 +21,11 @@ in opposition to the methode file_get_contents() https://stackoverflow.com/a/4402045/9497573 */ -function create_html($files, $vars) { +function create_html($files, $vars = null) { $files = (array)$files; $html_dir = PLUGIN_DIR.'html/'; - extract($vars); + if (!is_null($vars)) + extract($vars); ob_start(); foreach($files as $file) { diff --git a/plugins/wp_model_plugin/plugin_hooks.php b/plugins/wp_model_plugin/plugin_hooks.php index a0de4ad..5afcf39 100644 --- a/plugins/wp_model_plugin/plugin_hooks.php +++ b/plugins/wp_model_plugin/plugin_hooks.php @@ -93,11 +93,13 @@ function plugin_menu() { } add_action('admin_menu', 'plugin_menu'); -add_action( 'wp_ajax_nopriv_get_data', 'my_ajax_handler' ); -add_action( 'wp_ajax_get_data', 'my_ajax_handler' ); - function my_ajax_handler() { + console_log("in my_ajax_handler"); + console_log("data: "); + console_log($data); wp_send_json_success( 'It works' ); } +add_action( 'wp_ajax_get_data', 'my_ajax_handler' ); + ?>