WordPress has a function, wp_localize_script() that can do this. You create an array of values that you want to access in your JavaScript file and then inject it as an object.
You would modify your enqueue function like this:
function include_react_files() {
wp_enqueue_style( 'prefix-style', plugins_url('css/main.ae114d0c.css', __FILE__) );
// add the JS file to the footer - true as the last parameter
wp_register_script( 'plugin-scripts', plugins_url('js/main.d8745ba2.js', __FILE__),array(), '0.0.1', true );
$data = array(
'key1' => 'value1',
'key2' =? 'value2',
);
wp_localize_script( 'plugin-scripts', 'object', $data );
wp_enqueue_script( 'plugin-scripts' );
}
add_action( 'wp_enqueue_scripts', 'include_react_files' );
Then to access this data, you would simply do object.key1, object.key2, etc. So console.log( object.key1 ); will echo value1.
Related Posts:
- Given the ID of a product in woocommerce, how can I get its URL?
- How to delete all records from or empty a custom database table?
- Add Plugins to WordPress Theme
- Change the path where wordpress plugins are uploaded
- Add section (add_settings_section) to a custom page (add_submenu_page)
- Coding Challenge – JavaScript Easter Egg
- Using rewrite rule to access a view of a plugin from home page
- How to make a list of companies’ information and display them to user, using custom post types and a custom taxonomy?
- Thumb rating for wordpress – top user
- update_meta_user difference in i:1 and b:1, how to write b:1
- How to Get Admin Settings for a widget when using the_widget() with custom widget
- resize only height using wp_get_image_editor
- View the source code of a 3rd party plugin? [closed]
- How to crop image from center using wp_image_editor
- Deactivate plugin upon deactivation of another plugin
- How to find out what blocks are added by a plugin
- Shortcode in AJAX popup
- Set attached to state
- Use plugin or custom post type for game score functionality
- Converting Attachment to Images?
- How can I see the error generated by plugin activation?
- wordpress reusable content blocks
- How to add a random image to a post from gallery and only show one?
- Is there a filter for get_post_custom()?
- Problem with wordpress version 3.8
- Accessing post’s meta data based on user’s click of a post
- Stuck on image in an Advanced Custom Field loop
- license check in plugin
- With WordPress + Visual Composer, how to restrict a user to change some content in a page?
- How to append to title via functions.php for auto-posting plugin [duplicate]
- Translating long texts with html formatting
- Page Restriction and Redirect for Particular Levels or user
- Map Custom Registration Fields to WordPress User Roles
- Making Mathjax dynamic so it renders input without page refresh
- Front-end only $_POST and $_POST requests handling in WordPress plugin
- WordPress plugin/theme or other wayout to create portable pages?
- How to create user that has permission to create new sites on WordPress with multisite option enabled?
- How does WordPress know when there are plugin updates?
- WordPress Widget – Saving multidimensional arrays into $instance
- Add quantity field with each product on shop page-wooCommerce [closed]
- Does a blank plugin come with any overhead?
- Plugins fail to update: Download failed. A valid URL was not provided
- What is the right way to show reusable content
- URLs Added to ACF Repeater Field are not working
- Import Instagram post to WordPress blog post
- How to allow Contributors to publish articles after approval
- Making my plugin create a page?
- Not One ‘Contact Form Plugin’ will send email // Work
- PHP Warning: Declaration of aec_contributor_list::form() should be compatible with WP_Widget::form($instance)
- Why WP_Screeen doesn’t show all options with admin_body_class
- Built a second plugin but it overwrote the first one
- CMB2 not working in my WordPress theme
- Display Category Name of Current Post in WP Job Manager
- WordPress unable to write files in the server
- Preview with Custom Post Type Not Working
- Featured Image in RSS Feed
- Insert a Woocomerce product in specific product category
- How to disable tinyMCE button added by a plugin?
- Custom page in plugin visible in menu
- Disable WP Bakery plugin on specific page
- Error using wordpress functions inside a plugin class
- Preventing a missing or faulty plugin loaded inside of wp-settings from halting a script
- Get the link title attribute and send it google analytics as custom label via google tag manager using beehive pro for WordPress
- I have English words in Kurdish pages
- How can I add a custom Javascript snippet to display my feedback in footer?
- WordPress 5 boot-sequence / load-order
- Mixed content error after adding SSL certificate
- Meta Key array building with multiple input values from fields
- WordPress panel is not loading after plugin activated with error: HTTP ERROR 500
- Can’t activate and deactivate plugins
- Taxonomy Category Icon on index page
- Is it possible to change plugin’s Admin Panel Url?
- Only allow a plugin to work on a specific page? (Prefer not to use a plugin)
- Adding link for logged in user? [closed]
- Paid Memberships Pro, Display User Limit
- How to add dynamic content to posts page, archive, taxonomies, search, author, etc?
- Images is not showing after migration
- WordPress Development understanding core
- Input gets deleted/overwritten after changing to different Admin Menu
- Redirect loop when forwarding to mobile site
- Force Network Activated Plugin to Run After Site Level Plugins
- I want to add facility to add country, State, City in my custom plugin
- Drop down menu location select widget
- Cannot Search | Featured | Popular | Newest | Favorites in wordpress working
- Using Iris Colorpicker on TinyMCE shortcodes page [duplicate]
- WP-Snap too slow (caused by WP_Query?)
- Auto post with filling templates from external data and update periodical
- Need help finding the right plugin for a dynamic piece of content that appears multiple places
- Thumbnails and Lightbox
- Add a filter to a different custom plugin
- Integrate Mailchimp to a Contact Form 7 contact form [closed]
- Applying a coupon to a single product in woocommerce [closed]
- Allow users to download media files directly from Amazon S3 (AWS plugin)
- Override the plugin class and function
- Activate and deactivate two plugins automatically at certain hours
- Redux Framework slides field – How Can i add new placeholder Filed? [closed]
- Woocommerce business card flyer design [closed]
- Wp private content show me files attachments into post
- Remove H1 title in admin post edit screen
- How to test plugin development with Git Hub Actions (integration tests)?