wip ajax in menu with fetch

This commit is contained in:
asus
2024-02-09 23:56:26 +01:00
parent e0216ee5b3
commit 1e88b2c2f6
3 changed files with 42 additions and 1 deletions

View File

@@ -3,4 +3,11 @@
- this project uses submodules recursively, so after cloning you need to do :
`git submodule update --init --recursive`
- `wp_add_inline_script`
### todo :
- [read this guide to plugins development](https://developer.wordpress.org/plugins/)
- [learning about the rest api](https://developer.wordpress.org/plugins/settings/options-api/)
- ajax :
- [using fetch ajax](https://ricard.dev/how-to-use-wordpress-admin-ajax-with-fetch-api/)
- [wp ajax course](https://developer.wordpress.org/plugins/javascript/ajax/)
- [examples from stackoverflow](https://stackoverflow.com/questions/43557755/how-to-call-ajax-in-wordpress)
- using `admin_url('admin-ajax.php')` or `wp-utils`

View File

@@ -0,0 +1,27 @@
<?php
function wp_model_plugin_content() {
echo "
<input type='text' id='mytext'>
<button id='mybutton'>send</button>
<script>
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(url, {
method: 'POST',
credentials: 'same-origin',
body: JSON.stringify({ data: inputValue })
})
});
</script>
";
}
?>

View File

@@ -93,4 +93,11 @@ 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() {
wp_send_json_success( 'It works' );
}
?>