wip ajax in menu with fetch
This commit is contained in:
@@ -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`
|
||||
|
||||
27
plugins/wp_model_plugin/php/menu/menu.php
Normal file
27
plugins/wp_model_plugin/php/menu/menu.php
Normal 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>
|
||||
";
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -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' );
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user