How to pass PHP variables to JavaScript in WordPress
When you’re building a WordPress theme or plugin, sometimes you have JavaScript code that needs to use data/values from PHP. For instance, you need these values in your JavaScript code :
-
Homepage URL
Theme option values
WordPress posts data
etc
The easiest way to do that is by initializing those values into JavaScript objects in your header.php theme file or wp_head hook. For example :
<script> var myThemeParams = { homeURL: <?php echo home_url(); ?>, themeOptions: <?php echo get_theme_mod( 'mytheme_options', false ); ?>, } </script>
Even though it works but WordPress has been provided us with a function for doing something like that. It’s called wp_add_inline_script.
wp_add_inline_script( $handle, $data, $position = 'after' )
$handle
: Name of the script to add the inline script to.
$data
: String containing the JavaScript to be added.
$position
: Whether to add the inline script before the handle or after.
That function will add an inline script before or after your JavaScript code. It actually can do more besides passing PHP variables to JavaScript. You can see another use case here.
So, to use wp_add_inline_script for passing variables from PHP to JavaScript, you need to set the position properties to before so that it’ll add the inline script before your JS file. And then initialize a JavaScript object and set the value with data from PHP.
$myThemeParams = array( 'homeURL' => home_url(), 'themeOptions' => get_theme_mod( 'mytheme_options', false ) ); wp_enqueue_script( 'my-theme-script', get_template_directory_uri() . '/js/script.js' ); wp_add_inline_script( 'my-theme-script', 'var myThemeParams = ' . wp_json_encode( $myThemeParams ), 'before' );
In your JavaScript you can access it like this:
console.log( myThemeParams.homeURL ); console.log( myThemeParams.themeOptions );
Related Questions
- 2024-04-25 Apple said to be working on custom AI server chip based on TSMC’s 3nm process
- 2024-04-25 Researchers use supercapacitor components to build new sodium-ion battery
- 2022-08-20 Google is seeking to improve search results by preventing unoriginal content
- 2022-08-16 Microsoft will turn off TLS 1.0 and 1.1 in Internet Explorer and EdgeHTML on September 13
- 2022-08-15 Google releases Read Along for the web: Teaching children how to read
Your opinion
- forex 1楼
- Hi would you mind letting me know which hosting company you're working with? I've loaded your blog in 3 completely different web browsers and I must say this blog loads a lot faster then most. Can you recommend a good web hosting provider at a fair price? Thank you, I appreciate it!
- 发布于 2022/03/17 09:11
- Hot Ranking
-
- How to upgrade from Windows 10 to Windows 11 for free
- How to avoid duplicate form page submissions
- How to pass PHP variables to JavaScript in WordPress
- What is a weakly typed programming language
- Difference between line break markers and paragraph markers
- What is window.postMessage in HTML5?
- How to implement browser forward and backward functions for web form buttons
- The table layout is suddenly very confusing
- How To Determine Whether PHP Is Good for My Website or WordPress?
- What is a scripting language
- The role of the unordered list ul tag
- The impact of automatic caching on dynamic pages
- What is an XML document declaration?
- What values can be used for the background-position property?
- What is the difference between fixed and absolute?
- Latest Answer
-
-
How to avoid duplicate form page submissions
binary options system strategy page Answer 2022-04-01 03:42:00
-
How To Determine Whether PHP Is Good for My Website or WordPress?
แอพเปลี่ยนรูปเป็นภาพวาด Answer 2022-03-27 18:27:12
-
The impact of automatic caching on dynamic pages
50 deposit binary options Answer 2022-03-24 05:31:20
-
How to avoid duplicate form page submissions
free binary options signals providers of goods Answer 2022-03-20 12:29:02
-
How to avoid duplicate form page submissions
binary options strategy 5 minutes 15 min no loss acord Answer 2022-03-20 09:44:29
-