If your whole JavaScript code gets processed by PHP, then you can do it just like that.
If you have individual .js
files, and you don’t want PHP to process them (for example, for caching reasons), then you can just pass variables around in JavaScript.
For example, in your index.php
(or wherever you specify your layout), you’d do something like this:
<script type="text/javascript"> var my_var = <?php echo json_encode($my_var); ?>; </script>
You could then use my_var
in your JavaScript files.
This method also lets you pass other than just simple integer values, as json_encode()
also deals with arrays, strings, etc. correctly, serialising them into a format that JavaScript can use.
Related Posts:
- How to pop an alert message box using PHP?
- How to use php code in javascript
- How do I pass variables and data from PHP to JavaScript?
- Execute PHP function with onclick
- Mixed Content Page: requested an insecure stylesheet error
- SyntaxError: Unexpected token C in JSON at position 0 – Ionic 2 Http GET request
- SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
- is there something like isset of php in javascript/jQuery?
- how to call a onclick function in tag?
- How do I refresh a DIV content?
- Only on Firefox “Loading failed for the script with source”
- How to open a URL using PHP?
- Keep getting a “Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup” when attempting to google plus login on my web app
- how to write javascript code inside php
- Ajax success function
- AngularJS POST Fails: Response for preflight has invalid HTTP status code 404
- WordPress Bootstrap Handburger Menu Wont Open
- AJAX not Working with php as supposed
- Getting a variable inside foreach from PHP to JS after localization
- Open image gallery on link click
- AJAX request status 200 but no actual “response”
- Button insert link on front wp_editor not working
- Building a slide down search box in wordpress
- How Can I Add OnClick Event To A Specific WordPress Menu Link
- Base64 & JSON Encode array in PHP, use as HTML data attribute, decode and parse in JavaScript …. with proper Escaping
- Button not refreshing page
- Enqueue script if screen is greater than 500
- Ajax not working es expected (Returns 0)
- Using wp_localize_script inside template page to transfer var from php to js
- Use menu link or onClick to set a variable
- Preloader for a WordPress Site
- Trying to integrate Zoho Campaign Form [closed]
- Script Localization doesn’t work
- How to select the contents in the text view textrea in wp_editor?
- Creating a Multi-Level Associative Object Using AJAX
- Enqueue Javascript After ALL Other Scripts (Including Async Scripts)
- Push metadata in array
- WP functions in .js
- Adding custom directory and PHP files in wordpress
- I need to get an array from the wordpress database
- Enqueue js script to footer
- Enqueueing a script and a style sheet not working
- AngularJS with route and JSON API
- How to retrieve a value from an input field in a media view template
- Any required callbacks to WP.com or WP.org?
- Ajax – Call to undefined function get_option()
- How to add custom javascript into from custom plugin
- What’s the most efficient way to include this javascript in only a single post?
- Resolving Javascript errors likely related to Gantry framework
- JavaScript && operator in visual editor
- How to deal with the confliction between PHP-Markdown and MathJax?
- Pass data from wordpress to javascript in JSON
- How to make wordpress URLS google friendly for ajax driven sites?
- What is the “proper” way to generate a javascript variable depending on a custom field value?
- Adding a Javascript slideshow to the home page
- window.send_to_editor throws unterminated string literal error when adding image with multi line description
- remove inline scripts from the_content() produced by plugins?
- javascript conflict
- wordpress site | All-in-one-cufon IE Issue
- Help display image in single post using javascript zoom image..?
- How to disable inline css styles generated by Gutenberg editor?
- Populate dropdown with Child Pages based on Parent Page chosen
- Is there a way to check for an attribute of a script when using script_loader_tag?
- remove wp floating submenu in wp dashboard
- Programatically update posts in database from one block to another using transform
- How to conditionally show/hide controls & sections in WordPress customizer?
- Gutenberg getMedia() in post query doesn’t return all featured images
- InnerBlocks restricting does not work on nested blocks
- Can I change an external script’s HTML?
- Jquery function working in Chrome Console but not in the code [closed]
- How can I get my Calculate Contact Form 7 field to update with jQuery? [closed]
- How to transform a legacy widget into a block
- Gutenberg: Block validation Failed Richtext undefined
- Displaying admin notices inside the block editor from rest_after_insert_{$this->post_type} hook
- search and replace preword from author
- Add A Tag To A Post Through JavaScript In Gutenberg UI?
- Contact Form 7 – Uncaught TypeError: wpcf7.initForm is not a function [closed]
- How to prevent XSS alter custom global javascript object & methods in WordPress
- Gutenberg select categories
- Uppload image from another source wp.media
- Render blocking issue in google page insight
- contact form 7 repeatable fields calculations [closed]
- Add custom HTML markup to Gutenberg RichText
- js addclass function not working as expected, intereaction or special WP somethign needed?
- Listening change event of taxonomy term checkboxes in Gutenberg editor
- How to complete two other input fields, completed the first
- How to get the element ID from new menu list that added with add_filter()?
- Override Plugin Script Fucnction in WordPress
- Adding nonce or hashes to inline scripts
- Enqueue scripts all over but not in single.php
- Status 400 for AJAX POST Request with admin-ajax.php
- How to inject variables into public-facing JS using wp_enqueue_script
- How to pass aa JS variable to PHP?
- List of JS Events for Widgets?
- Customize Theme comment template to Insert VoteUp and VoteDown buttons
- Authentication with the Rest API when using an External Application
- Search media with javascript
- Ajax random number always the same
- Image upload via FormData API and AJAX is not working ($_FILES always empty)
- How to set callback to execute after all the tinyMCE editors have been initialized?