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:
- Does anybody know how to insert a complete jquery plugin into a wordpress elementor-page?
- JS working when used normally but not in wordpress
- How can I add a custom Javascript snippet to display my feedback in footer?
- How can I create a secure file download in WordPress?
- front end editor creation for Restropress plug in – displaying information from a WP admin area, on a different URL
- Use AJAX to fetch Current Post Thumbnail for WordPress when Uploaded throughMedia Uploader Frontend
- WordPress site hamburger menu toggle not working
- Website always load twice on start
- How to add defer to WordPress Plugin javascript? [duplicate]
- Add external javascript to post template
- Problem with wordpress has_action() function
- How to get Angular app working in WordPress plugin?
- Multiple image selection custom widget
- Execute inline javascript in wordpress after page fully loaded
- Why can I not deregister my style and script on custom post type?
- Custom Field used to allow a Free Story; no longer works
- Create a form and have custom menu display based on user answers?
- 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
- Javascript inserted from Plugin pointing to wrong location
- How to solve Blocked a frame with origin from accessing a cross origin frame error in wordpress?
- WordPress ajax error 400 bad request for sending data to remote site [duplicate]
- Enabling plugin on specific pages and subpages
- Speeding up WordPress on a Local Network
- How to use information from the database in the front-end?
- Conditional Fields depending on checkboxes
- Custom plugin giving: wp-admin/admin-ajax.php 400 (Bad Request)
- 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
- How to pass value from WordPress to Javascript
- how to send two forms with one click (script ninjaforms id)
- wp_enqueue_scrpts seems not to get called
- How to print shortcode with js in visual composer?
- Number format in thousands on highcharts in wpdatatable plugin
- Link in navigation menu send user to different page if mobile?
- Facebook Quote Plugin for Blockquote?
- Disqus comments count taking long to update
- Adding javascript to header of MetroMagazine theme
- Embed Javascript code to registered users only
- show-hide based on select option jquery wordpress
- Speed issues because of CSS & JavaScript
- Having trouble going from Jfiddle to implementing in WordPress :(
- Javascript in plugin not working
- How to limit each front-end user to view just his own uploaded files on Amazon S3?
- How to create a dashboard for logged-in users with custom functionality? [closed]
- Change localization only for plugin
- Best use adding user generated JS/CSS to a theme with a plugin
- Creating a user ‘add custom field’ section
- Javascript not working
- Jetpack Comments Change Placeholder Text
- Locale changed but plugin still showing default language
- Creating custom URL for async content
- Media Playlist Plugin
- Need to hide an element depending on date and post category
- Adding an onchange event to a custom dropdown menu in WordPress
- How to set up php,javascript and css to allow for our finished product to be displayed within a page?
- How do I convince this button to do something when it is clicked?
- Possible jquery / library conflict in plugin usage
- Ajax, jQuery and WordPress
- Plugin and javascript placement
- Why won’t the Comprehensive Google Map Plugin load?
- Plugin – including external javascript calling google maps
- Internationalization of Blog Posts
- get_option include in my adsense
- contact 7 plugin stops some pages from working properly
- Expanding / Collapsing Modules in WordPress Panel
- Can’t insert files in other inputs
- Is it possible to run javascript on plugin deactivated?
- How do I output user_registered time in my correct timezone?
- Ajax in Plugins: returns the whole page
- Move specific javascript to the footer
- Element works, but not on wordpress site [duplicate]
- Load css classes after using ajax calls
- How to tweak a plugin without preventing it from updating
- 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
- Eliminate Render blocking CSS?
- making a simple plugin
- TypeErrors with Yoast, jQuery in conflict?
- Javascript as Jquery Function Call?
- How to make a rest style plugin?
- Defer parsing of JavaScript [duplicate]
- How do I put a word-press blog into my static site without installing wordpress on server?
- How can I include properly javascript files in my wp plugin [duplicate]
- Count div with class (.length) only shows 0 [closed]
- Adding a script & php to functions.php
- how to embed a crop feature for cropping images uploaded by the user
- how to show selected options drop down menu values in attributes field in after saving post.php
- How to access values from your own settings page in javascript?
- Refresh server side rendered block in Gutenberg without changing attributes or its content
- Beginner question: Accessing js script in plugin
- How do I call an action hook into wp_ajax hook callback function
- I want to display data last 7 days with date wise in chartjs?
- Select posts from list and add them in a new list
- Admin Notice is only localized when displaying the “Plugins” Backend Page