- wip ajax

- already added ajax url to the front object
This commit is contained in:
asus
2024-02-09 18:06:06 +01:00
parent 011f0b9c3f
commit e0216ee5b3
3 changed files with 6 additions and 16 deletions

View File

@@ -1,4 +1,4 @@
const title = document.querySelector(".first_el_to_change"); const title = document.querySelector(".first_el_to_change");
title.innerHTML = "--- coucou ;) " + php_data.myvar_1; title.innerHTML = "--- coucou ;) " + php_data.myvar_1;
console.log(php_data.myvar_1); console.log(php_data);

View File

@@ -40,6 +40,7 @@ function add_files_to_front($files_arr) {
/* /*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
pass variables to front as global variables, accessible in js as an object pass variables to front as global variables, accessible in js as an object
it also adds the ajax url
@param two params : @param two params :
1. an array of key => value 1. an array of key => value
with the key being name of the variable, like this : with the key being name of the variable, like this :
@@ -49,27 +50,15 @@ pass variables to front as global variables, accessible in js as an object
2. name of first embended script (if we want to have the variables 2. name of first embended script (if we want to have the variables
availables to all js : it will be available to the script and all following) availables to all js : it will be available to the script and all following)
- default value is "init", assuming the first script is called "init.js" - default value is "init", assuming the first script is called "init.js"
old version :
- was using wp_add_inline_script() which is rather for scripts than variables
- it had to write explicitly the variable declaration as "const myvar = 'value'"
*/ */
function add_var_to_front($vars, $handle = "init") { function add_var_to_front($vars, $handle = "init") {
$handle = pathinfo($handle, PATHINFO_FILENAME); $handle = pathinfo($handle, PATHINFO_FILENAME);
$object_name = "php_data"; $object_name = "php_data";
$ajax_url = admin_url( 'admin-ajax.php' );
$vars["ajax_url"] = $ajax_url;
wp_localize_script($handle, $object_name, $vars); wp_localize_script($handle, $object_name, $vars);
} }
// function add_var_to_front($vars, $handle = "init") {
// extract($vars);
// $handle = pathinfo($handle, PATHINFO_FILENAME);
//
// foreach ($vars as $key => $var)
// {
// $js_var = 'const ' . $key . ' = ';
// $js_var .= json_encode($var);
// $js_var .= ';';
// wp_add_inline_script($handle, $js_var, 'before');
// }
// }
?> ?>

View File

@@ -80,6 +80,7 @@ add_shortcode('wp_model_plugin', 'main_shortcode');
/* /*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
menu plugin menu plugin
ajax : https://stackoverflow.com/questions/43557755/how-to-call-ajax-in-wordpress
*/ */
function plugin_menu() { function plugin_menu() {
add_menu_page( add_menu_page(