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
- force enqueue script to be first in order of prominence
- Modifying a JS file with data from plugin settings
- A better way to override plugin’s JS files?
- How can I reduce the amount of files loaded/included per plugin?
- How to call plugin path in JS?
- How do I sanitize a javascript text?
- How can a Firebase user registration and login be integrated into a WordPress site?
- How do I make my wordpress page more friendly for mobile viewers?
- AJAX Call Via Vanilla JavaScript In WordPress Plugin Development
- Custom plugin giving: wp-admin/admin-ajax.php 400 (Bad Request)
- How can I make content disappear when a user logs in?
- How to save Clicks of a download link button while it doesn’t matter if we refresh the page or again login/logout
- ‘Lazy loading’ list of posts with pagination
- Check javascript file Proper way in functions.php
- How to include admin-ajax when loading external javascript
- how would I include a js file with tags into wordpress?
- Dequeue / Deregister script and replace it with a new plugin
- Google Maps and Yandex Maps doesn’t work correctly
- Prevent CSS/JS loading when plugin is not used
- Creating an Exit Confirmation Popup
- How to create popup same as wordpress popup feature
- Gutenberg List Extension Block
- Localiztion in javascript
- Remove caching from wp_remote_get calls from custom plugin
- prettyPhoto media loading very slowly in Chrome
- How many SQL queries WP Super Cache make to serve cached page?
- Pulling data from custom plugin settings using PHP shortcode and Javascript
- Using custom JS plugins with WordPress?
- Override Javascript in a Plugin?
- Change upload folder for a CPT
- add input in custom javascript from WordPress pages/posts
- Redux Framework: Page freezes on typing in Editor
- Using window.onload with Ubermenu
- My homemade plugin is trying to update to someone else’s plugin
- Is there a loading priority for login_enqueue_scripts?
- Delay JavaScript files from loading
- “Conflict” with jQuery (or its plugins)?
- Can WordPress Drop-ins be given their own directory?
- How Can I Safely Remove WP-Super Cache without messing my site up? [closed]
- How to execute a plugin on a single page only?
- MediaElement.js works on one site but not the other?
- Pass user role to javascript code inside body
- How to add support for caching plugins for my own plugin?
- How to get specific string/value from an array? php [closed]
- Access post title from custom meta box on title change
- Background Music WP [closed]
- Why does my jQuery plugin show up as text in WordPress?
- Adding a simple Javascript file with a plugin
- Social share buttons text shows up on post excerpts
- Dropdown menu on click change
- How do I override all stylesheets and scripts without a plugin
- Plugin Admin panel checkbox unchecked after submit
- Send Array from JS to php file then insert it in wp database through a plugin
- How to override Plugin javascript function : $(‘body’).on(‘change’,’.class’)
- Connect / Login WordPress Hosted Website or Blog with NodeJs
- login form should redirect to register page for in 1st login next time it should redirected to home page
- Javascript embedded disappears for editors
- How to fetch WordPress database values in JavaScript widget
- Possible way of having multiple Gutenberg instances for a page?
- Woocommerce add products to cart
- Show popup on click
- Javascript not getting injected into page
- Why does my custom plugin only function correctly once per page?
- How can I create a secure file download in WordPress?
- Use AJAX to fetch Current Post Thumbnail for WordPress when Uploaded throughMedia Uploader Frontend
- WordPress site hamburger menu toggle not working
- WordPress logout appends cache/all/index.php to the site_url()
- Woocommerce – Change variations output
- How to disable/enable PHP plugin functionality based on a TinyMCE toggle-button
- WordPress ajax error 400 bad request for sending data to remote site [duplicate]
- Customizer preview URL not updating when settings reset to default
- wp_enqueue_scrpts seems not to get called
- show-hide based on select option jquery wordpress
- Speed issues because of CSS & JavaScript
- Having trouble going from Jfiddle to implementing in WordPress :(
- How to create a dashboard for logged-in users with custom functionality? [closed]
- Should I Save widget HTML to a file cache?
- Creating a user ‘add custom field’ section
- Javascript not working
- Creating custom URL for async content
- Adding an onchange event to a custom dropdown menu in WordPress
- How do I convince this button to do something when it is clicked?
- Session alternative for plugins (due to caching)
- Plugin – including external javascript calling google maps
- W3 Total Cache and IIS7 not doing much [closed]
- Element works, but not on wordpress site [duplicate]
- how woocommerce swatch color name when hovered or selected
- How to create post comparison in wordpress
- WordPress shortcode plugin not working
- FacetWP: Plugin breaks button/modal functionality inside searchable content area
- TypeErrors with Yoast, jQuery in conflict?
- Javascript as Jquery Function Call?
- Count div with class (.length) only shows 0 [closed]
- Fragmented Static Conversion of WordPress Pages
- Beginner question: Accessing js script in plugin
- Select posts from list and add them in a new list