WordPress has a nice function mainly for that wp_localize_script
To use it first queue your script:
wp_enqueue_script( 'My_Script_handle', 'path/to/script.js' );
then create an array of strings you want to localize:
$data = array(
'exit' => __( 'Exit','my-plugin-domain' ),
'open' => __( 'Open','my-plugin-domain' ),
'close' => __( 'Close','my-plugin-domain' ),
'next' => __( 'Next','my-plugin-domain' ),
'previous' => __( 'Previous','my-plugin-domain' )
);
and call it using wp_localize_script
wp_localize_script( 'My_Script_handle', 'mystrings', $data );
then you can access it in the page using JavaScript like this:
alert(mystrings.exit);
alert(mystrings.open);
…
you get the Idea.
Related Posts:
- force enqueue script to be first in order of prominence
- Modifying a JS file with data from plugin settings
- Using plural-only translation of register_post_status() in 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?
- Plugin readme.txt and assets internationalization
- 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?
- 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
- Making my plugin translation ready. Poedit not working
- Possible to add a language file to a plugin without adding .po/.mo files to plugin directory?
- Change upload folder for a CPT
- Override Plugin Script Fucnction in WordPress
- What can be reason for no plugin-strings available under stable section on translate.wordpress.org?
- Force language in translate function
- How to get DEFER or ASYNC attribute to appear when I add a stylesheet with wp_enqueue_style from a plugin? [duplicate]
- Using window.onload with Ubermenu
- GeoMashup plugin: how to customize cluster markers / icons? [closed]
- Is there a loading priority for login_enqueue_scripts?
- Delay JavaScript files from loading
- “Conflict” with jQuery (or its plugins)?
- how to prevent all css and javascripts files from loading
- Pass user role to javascript code inside body
- Setting Email locale before retrieving gettext translations
- 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?
- Disable submit button order
- Social share buttons text shows up on post excerpts
- Replace woocommerce | (pipe) separator in product variation to , or another character?
- Dropdown menu on click change
- Localise settings section headline
- CSS/JS files in WordPress
- How do I override all stylesheets and scripts without a plugin
- Plugin Admin panel checkbox unchecked after submit
- Custom Plugin scripts from admin working in frontend
- Ajax call not working in wordpress through a plugin
- Send Array from JS to php file then insert it in wp database through a plugin
- Gutenberg: Prevent links from being clickable in the block. Error: “Cannot destructure property ‘frameElement’ of ‘r’ as it is null.”
- How to override Plugin javascript function : $(‘body’).on(‘change’,’.class’)
- Connect / Login WordPress Hosted Website or Blog with NodeJs
- Cannot run script properly with my custom plugin
- 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
- how i retreive the post content while editing it in real time however the editor is tinymce or gutenberg
- How to properly enqueue jQuery knob on WordPress without conflict?
- Javascript not getting injected into page
- Why does my custom plugin only function correctly once per page?
- How to get a specific string from a url
- Does anybody know how to insert a complete jquery plugin into a wordpress elementor-page?
- JS working when used normally but not in wordpress
- Use AJAX to fetch Current Post Thumbnail for WordPress when Uploaded throughMedia Uploader Frontend
- Add external javascript to post template
- Problem with wordpress has_action() function
- AJAX update fails for public/non-admin users
- Woocommerce – Change variations output
- I dont know why my localization is not working
- 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
- Caching of combined CSS and JS fails because of losing MIME types through combining by PHP
- wp_enqueue_scrpts seems not to get called
- Adding javascript to header of MetroMagazine theme
- 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]
- Change localization only for plugin
- Creating a user ‘add custom field’ section
- Javascript not working
- Creating custom URL for async content
- Media Playlist Plugin
- How do I convince this button to do something when it is clicked?
- Why won’t the Comprehensive Google Map Plugin load?
- Plugin – including external javascript calling google maps
- Element works, but not on wordpress site [duplicate]
- how woocommerce swatch color name when hovered or selected
- Loading Canvas & WooCommerce translation file in child theme
- 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?
- Count div with class (.length) only shows 0 [closed]
- Beginner question: Accessing js script in plugin
- Admin Notice is only localized when displaying the “Plugins” Backend Page