Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read global variable from WP-Config.php in JS file

I have defined URL in wp-config file.

DEFINE('URL', 'google.com');

Now i want to access this URL from my JS theme file:

<script> alert(URL); </script>

How to do this?

like image 806
Sz3jdii Avatar asked Oct 21 '25 03:10

Sz3jdii


2 Answers

use localize in functions.php

add_action('wp_enqueue_scripts' , function(){ 
  wp_localize_script('jquery', 'config_var', URL );
});

and in js file => config_var will equal the config variable value

like image 171
Ash0ur Avatar answered Oct 23 '25 20:10

Ash0ur


You have to put a bit of your JS in your PHP file (i.e. use script tags to accomplish this). Then do the following:

  1. Place the script in your php file
  2. <script> alert(<?php echo URL ?>); </script>
like image 21
Willem van der Veen Avatar answered Oct 23 '25 18:10

Willem van der Veen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!