I think you should in this case use echo:
function print_script() {
echo '<script type="text/javascript">
window.onload = function () {
var loadTime = window.performance.timing.domContentLoadedEventEnd-window.performance.timing.navigationStart;
alert(loadTime);
console.log(loadTime);
}
var var1 = <?php echo json_encode('var1'); ?>;
var var2 = <?php echo json_encode('var2'); ?>;
var var3 = <?php echo json_encode('var3'); ?>;
</script>';
}
add_action('wp_print_scripts', 'print_script');
Related Posts:
- Why my browser keeps loading old version of custom JavaScript files in \divi\js? [closed]
- W3 Total Cache: Load CSS asynchronously for better PageSpeed score? [closed]
- How do I minify a plugins CSS/JS code?
- Caching of combined CSS and JS fails because of losing MIME types through combining by PHP
- Sudden ERR_HTTP2_PROTOCOL_ERROR on every JS and CSS files loads
- Pulling data from custom plugin settings using PHP shortcode and Javascript
- With W3 Total Cache when I publish a post it does not appear in the homepage. Only if i purge all cache [closed]
- Using custom JS plugins with WordPress?
- Override Javascript in a Plugin?
- How to disable specific plugin in mobile version?
- Is there an earlier hook than login_head or login_enqueue_scripts?
- Best approach to fetch data from wp options to js file or php file
- Correct way to perform non-cacheable DB query
- Can someone please tell me what is wrong with my plugin?
- Updating the Drag-To-Share eXtended share URLs?
- Scripts at the end of the page
- wordpress site | All-in-one-cufon IE Issue
- Enqueue WordPress plugin scripts below all other JS
- How to add Internationalization in WordPress using Javascript/React?
- Plug-in Conflict: Art Direction and Super Cache
- Can I use custom CSS and js plugin to put JavaScript in to validate my forms
- Change upload folder for a CPT
- How to get the element ID from new menu list that added with add_filter()?
- Override Plugin Script Fucnction in WordPress
- Time consuming callbacks in customizer
- Modifying a .js Function for Print [QZ Tray, WooCommerce POS]
- changes to widget settings not showing up in individual pages pages until they’re updated [closed]
- add image crop function like in wp customizer
- Calculate price and display on woocommerce product single page under price (simple price, variable price) [closed]
- Executing Javascript when a New Post is Published
- Set featured image using javascript in post editor
- Display encrypted content on my website
- Is there a standard way to listen for an on change event of a posts featured image
- How to edit content before post update
- Trying to create a private area custom for each user where they upload images to a server [closed]
- create link/button to purge cloudflare cache on post
- wp.media javascript issue with on select
- WordPress + JavaScipt + AJAX + MySQL: insert query for form
- add input in custom javascript from WordPress pages/posts
- Advanced AJAX Page Loader ignores other JS code
- How to insert HTML/JavaScript form into WordPress page? [closed]
- accessing wp.media api from a tinymce plugin
- Add custom JavaScript that is a combination of CDN/uploaded files and easily-edited code?
- How to load wp_editor via AJAX
- Redux Framework: Page freezes on typing in Editor
- How to get DEFER or ASYNC attribute to appear when I add a stylesheet with wp_enqueue_style from a plugin? [duplicate]
- How to get my plug-in, wrote in php, to refresh on its own every 5 minutes?
- WordPress 3.8.3. custom theme – sliders won’t load js/css files
- Using TinyMCE in plugin?
- How to handle cookies from a WordPress plugin on a cached page?
- Using window.onload with Ubermenu
- w3 total cache and sitemap.xml
- Caching RSS plugin data
- Adding (image) caching to my Tumblr widget
- GeoMashup plugin: how to customize cluster markers / icons? [closed]
- w3 Total Cache not Caching – Requested URI is rejected [closed]
- How can I remove the WordPress-Version (?ver=5.x) from my plugin
- Redirect to another page using contact form 7? [closed]
- My homemade plugin is trying to update to someone else’s plugin
- Is there a loading priority for login_enqueue_scripts?
- _prime_post_caches in wordpress
- Delay JavaScript files from loading
- “Conflict” with jQuery (or its plugins)?
- JavaScript in a PHP plugin
- Combining several CSS files into one for optimization
- Deactivate JS Script in Plugin Shortcode
- how to prevent all css and javascripts files from loading
- Remove / overwrite some functions in buddypress.js
- wordpress add_action() issue in ajax call
- How do I add some javascript validation to the admin interface form’s onsubmit?
- Can WordPress Drop-ins be given their own directory?
- How Can I Safely Remove WP-Super Cache without messing my site up? [closed]
- Localizing methods for jQuery append prepend wrap parent
- How to execute a plugin on a single page only?
- how to insert a HTML form into a javascript popup?
- Display a consistent Google Plus icon on ALL pages of my site
- MediaElement.js works on one site but not the other?
- Which plugin can do horizontal scrolling of RSS feeds in WordPress [closed]
- What is the best way to cache pages on WordPress
- Pass user role to javascript code inside body
- Add type to enqueued script inside plugin
- After updating the WordPress getting a syntax error in the console
- Changes not visible when not logged in
- How to generate an all in one WordPress New content, plugin and theme update report on a website? [closed]
- How to add support for caching plugins for my own plugin?
- get specific value of a array | PHP
- How to get specific string/value from an array? php [closed]
- coding a WordPress AJAX Form using PHP to check if User is Logged Out and Show error
- How use wp media upload liberary in react components?
- Access post title from custom meta box on title change
- Background Music WP [closed]
- How to use custom Javascript code inside a plugin?
- Why does my jQuery plugin show up as text in WordPress?
- Adding a simple Javascript file with a plugin
- JQuery needs to be defined in , but Gravity Forms is defining it in the
- site url is not working after migrtaion in worpress, all links are broken
- Object Caching Plugin force every plugin to cache objects?
- Disable submit button order
- How to save comment name email url fields?
- Conditional custom redirect