wip trying to get the var name directly, first try not working
This commit is contained in:
@@ -44,9 +44,13 @@ function main_shortcode() {
|
|||||||
# https://wiki.php.net/rfc/nameof
|
# https://wiki.php.net/rfc/nameof
|
||||||
# but for now i have to declare both the variable and its name
|
# but for now i have to declare both the variable and its name
|
||||||
# (more : https://stackoverflow.com/q/255312/9497573)
|
# (more : https://stackoverflow.com/q/255312/9497573)
|
||||||
|
# add_var_to_front( array(
|
||||||
|
# "myvar_1" => $myvar_1,
|
||||||
|
# "myvar_2" => $myvar_2,
|
||||||
|
# ));
|
||||||
add_var_to_front( array(
|
add_var_to_front( array(
|
||||||
"myvar_1" => $myvar_1,
|
$myvar_1,
|
||||||
"myvar_2" => $myvar_2,
|
$myvar_2,
|
||||||
));
|
));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,10 +28,28 @@ function add_script_to_front($script_files_arr) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# https://stackoverflow.com/a/61442261/9497573
|
||||||
|
function get_varname(&$var) {
|
||||||
|
$tmp = $var; // store the variable value
|
||||||
|
$var = '_$_%&33xc$%^*7awdlawidawd__D:DWD2398q_r4'; // give the variable a new unique value
|
||||||
|
$name = array_search($var, $GLOBALS); // search $GLOBALS for that unique value and return the key(variable)
|
||||||
|
$var = $tmp; // restore the variable old value
|
||||||
|
console_log("name : " . $name);
|
||||||
|
return $name;
|
||||||
|
}
|
||||||
|
|
||||||
function add_var_to_front($var_array) {
|
function add_var_to_front($var_array) {
|
||||||
foreach ($var_array as $key => $var)
|
# foreach ($var_array as $key => $var)
|
||||||
|
# {
|
||||||
|
# $js_var = 'const ' . $key . ' = ';
|
||||||
|
# $js_var .= json_encode($var);
|
||||||
|
# $js_var .= ';';
|
||||||
|
# console_log("in php, js_var: " . $js_var);
|
||||||
|
# wp_add_inline_script('myscript', $js_var, 'before');
|
||||||
|
# }
|
||||||
|
foreach ($var_array as $var)
|
||||||
{
|
{
|
||||||
$js_var = 'const ' . $key . ' = ';
|
$js_var = 'const ' . get_varname($var) . ' = ';
|
||||||
$js_var .= json_encode($var);
|
$js_var .= json_encode($var);
|
||||||
$js_var .= ';';
|
$js_var .= ';';
|
||||||
console_log("in php, js_var: " . $js_var);
|
console_log("in php, js_var: " . $js_var);
|
||||||
|
|||||||
Reference in New Issue
Block a user