Hi,
I want to use a PHP variable from a php file in a javascript file. I found several methods on the Net but nothing works.
It would be great if you could help me.
Thanks in advance.
Best regards
Albert
Hi,
I want to use a PHP variable from a php file in a javascript file. I found several methods on the Net but nothing works.
It would be great if you could help me.
Thanks in advance.
Best regards
Albert
Welcome to the forums @albertkastl
What have you tried thatâs not worked?
For example Iâve loaded into the PHP file an external JS file with the content
var puzzlemin = '<?php echo $puzzlemin?>';
and used then puzzlemin in the JS file.
I think some example code of that would be helpful
For instance a simple test, this works
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>Document</title>
</head>
<body>
<?php
$x = 5;
?>
<script>
const x = <?php echo $x ?>;
console.log('X is', x) // X is 5
</script>
</body>
</html>
Itâs going to be the same thing, except that what youâd have to do to make it external is to make your âJS fileâ a .php file.
Thanks rpg_digital. But this is in ONE file.
To be more precise:
I loaded in the PHP file with âwp_enqueue_scriptâ the mentioned JS file AND another JS file in which I wanted to use the PHP variable.
I wanted to change a Wordpress plugin. Therefore I have to use the separate JS file.
JS.php
<?php $x = 5; ?>
const x = <?= $x ?>;
console.log(x);
index.php (or .html)
<html>
<head>
<script src='JS.php'></script>
...
Your browser wont care what the extension of the file is - but the server does.
With wp_enqueue_script()
this is a slightly different matter though as you probably want access to variables defined within your wordpress instance, not just any PHP script floating around. The âwordpress wayâ to do this is using wp_localize_script()
â as the name suggests, itâs actually intended for localization but can be used to expose any objects to your JS, for instance from your functions.php
:
wp_enqueue_script('my-script', get_template_directory_uri() . '/js/my-script.js');
wp_localize_script('my-script', 'something', ['foo' => 'bar']);
Then in my-script.js
:
/* global something */
console.log(something) // -> { foo: 'bar' }
Under the hood this does the same as in the examples already given, it defines a global variable on the window
object â but only if the given script is actually getting enqueued, and it leaves the nasty <?php echo ... ?>
inside JS to the wp internals. :-)
Thanks m3g4p0p for the detailed help.
But for me as a beginner who tries to customize a Wordpress plugin, all the connections Wordpress, Php, Javascript, CSS are very confusing. So, please be patient with me.
Could you please explain what I have to put in âsomethingâ and inside the brackets in
wp_localize_script('my-script', 'something', ['foo' => 'bar']);
The 2nd argument is the variable name for your JS, and the 3rd argument (with the brackets) is an associative array with the data that will be assigned to that variable as a JS object. Hereâs a somewhat more elaborate example:
$name = 'John';
$age = 42;
wp_localize_script('my-script', 'person', [
'name' => $name,
'age' => $age
]);
my-script.js
/* global person */
alert(person.name + ' is ' + person.age + ' years old')
And when in âmy-scriptâ I have
var puzzlemin = '<?php echo $puzzlemin?>';
how would then âwp_localize_scriptâ look like?
Pass $puzzlemin
the same way as $name
or $age
in the above example, and it will be available as a property of the object with the provided name⌠say
PHP
wp_localize_script('my-script', 'puzzleData', ['puzzlemin' => $puzzlemin]);
JS
console.log(puzzleData.puzzlemin)
Thanks a lot m3g4p0p!
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.