diff --git a/plugins/wp_model_plugin/html/index.html b/plugins/wp_model_plugin/html/index.html
index 529c290..d034d64 100644
--- a/plugins/wp_model_plugin/html/index.html
+++ b/plugins/wp_model_plugin/html/index.html
@@ -1,6 +1,7 @@
i am a new p
to change
+
{
+ ajax_post(ajax_button_1, 'get_data');
+});
diff --git a/plugins/wp_model_plugin/js/menu/menu.js b/plugins/wp_model_plugin/js/menu/menu.js
index 88b3bb9..206c5b8 100644
--- a/plugins/wp_model_plugin/js/menu/menu.js
+++ b/plugins/wp_model_plugin/js/menu/menu.js
@@ -3,6 +3,5 @@ const sendButton = document.getElementById('mybutton');
sendButton.addEventListener('click', () => {
const inputValue = inputElement.value;
-
ajax_post(inputValue, 'get_data');
});
diff --git a/plugins/wp_model_plugin/js/myscript3.js b/plugins/wp_model_plugin/js/myscript3.js
index 2361df5..7bee422 100644
--- a/plugins/wp_model_plugin/js/myscript3.js
+++ b/plugins/wp_model_plugin/js/myscript3.js
@@ -1,4 +1,4 @@
const title3 = document.querySelector(".third_el_to_change");
-title3.innerHTML = "--- bye bye, " + php_data.myvar_2;
-console.log(php_data.myvar_2);
+title3.innerHTML = "--- bye bye, " + myvar_2;
+console.log(myvar_2);
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 ec0f600..5b3f84b 100644
--- a/plugins/wp_model_plugin/php/utils/add_to_front.php
+++ b/plugins/wp_model_plugin/php/utils/add_to_front.php
@@ -7,6 +7,7 @@
function add_files_to_front($files_arr) {
//wp_enqueue_script(
, /url/to/file, [depends on], version, defer? );
//wp_enqueue_style( , /url/to/file, [depends on], version, media );
+ global $first_script;
// always adding ajax file first
$files_arr = ["ajax.js", ...$files_arr];
@@ -28,6 +29,8 @@ function add_files_to_front($files_arr) {
$file_version = date("ymd-Gis", filemtime($file_path));
if ($file_ext === "js") {
+ if (is_null($first_script))
+ $first_script = $file_basename;
wp_enqueue_script( $file_basename, $file_url, $previous_js_basename, $file_version, true);
$previous_js_basename = $file_basename;
}
@@ -52,24 +55,41 @@ function add_files_to_front($files_arr) {
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-pass variables to front as global variables, accessible in js as an object
-it also adds the ajax url
+pass variables to js front as global variables
@param two params :
1. an array of key => value
with the key being name of the variable, like this :
'my_var' => 'value',
simpler way to do it is to use compact when calling the function :
add_var_to_front(compact("var1", "var2", "var3"));
-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)
- - default value is "ajax", assuming the first script is "ajax.js"
- since it's automatically added first by `add_files_to_front`
+2. (optionnal) name of first embended script that need these variables
+ (it will be available to this script and all followings)
*/
-function add_var_to_front($vars, $handle = "ajax") {
+function add_var_to_front($vars, $handle = null) {
+ if (is_null($handle)) {
+ global $first_script;
+ $handle = $first_script;
+ }
$handle = pathinfo($handle, PATHINFO_FILENAME);
- $object_name = "php_data";
-
- wp_localize_script($handle, $object_name, $vars);
+
+ extract($vars);
+ foreach ($vars as $key => $var)
+ {
+ $js_var = 'const ' . $key . ' = ';
+ $js_var .= json_encode($var);
+ $js_var .= ';';
+ wp_add_inline_script($handle, $js_var, 'before');
+ }
+//
+// the other way with localize has multiple incidences :
+// - it creates an object from wich you can access the variables
+// - so if you call it again wiht the same name, it will overwrite the previous
+// {
+// $handle = pathinfo($handle, PATHINFO_FILENAME);
+// $object_name = "php_data";
+//
+// 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 bdef3c0..235ddef 100644
--- a/plugins/wp_model_plugin/php/utils/create_html.php
+++ b/plugins/wp_model_plugin/php/utils/create_html.php
@@ -24,8 +24,6 @@ https://stackoverflow.com/a/4402045/9497573
function create_html($files, $vars = null) {
$files = (array)$files;
$html_dir = PLUGIN_DIR.'html/';
- console_log("vars: ");
- console_log($vars);
if (!is_null($vars))
extract($vars);
diff --git a/plugins/wp_model_plugin/php/utils/globals.php b/plugins/wp_model_plugin/php/utils/globals.php
index f0c1e83..c6cdcb8 100644
--- a/plugins/wp_model_plugin/php/utils/globals.php
+++ b/plugins/wp_model_plugin/php/utils/globals.php
@@ -11,4 +11,9 @@ const CONSOLE_OFF = true;
*/
const CONSOLE_OFF = false;
+/* switch console_log
+const CONSOLE_OFF = true;
+*/
+$first_script = null;
+
?>
diff --git a/plugins/wp_model_plugin/plugin_hooks.php b/plugins/wp_model_plugin/plugin_hooks.php
index 020aec1..d2ec1bc 100644
--- a/plugins/wp_model_plugin/plugin_hooks.php
+++ b/plugins/wp_model_plugin/plugin_hooks.php
@@ -70,6 +70,10 @@ function main_shortcode() {
)
);
+ global $wp_scripts;
+ console_log("wp_scripts: ");
+ console_log($wp_scripts);
+
return $html_front;
}
add_shortcode('wp_model_plugin', 'main_shortcode');