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:
- Unable to register rewrite rule using add_rewrite_rule
- how to compare date meta value in jet smart filter Date query?
- Let a user change role dynamically on the front end
- is possible to edit diagrams inside wordpress
- Help with coding a link for a button in WordPress with The Events Calendar Plugin
- Sudden ERR_HTTP2_PROTOCOL_ERROR on every JS and CSS files loads
- Swoof filter settings
- How to change css poperty through webhook in wordpress
- Create index for own custom data in elasticpress from wordpress plugin
- WordPress: customize caption inside the fancybox slide with html elements
- How to resolve status code: 410 from Pagespeed insights for the wordpress website
- Looking for Features in WP Compatible Slider
- Fatal Error in my WP “Call to undefined function get_option()”
- Plugin Error – Not Sure Where to Go From Here
- Issue using form in Google app in mobile
- How to check if something belongs to a list or not
- Dropdown menu only appearing when logged in
- No WP-Content Folder in Website Files, No Ability to Disable Plugins Manually
- Can The REST API be used to create/edit plugins and themes?
- Product image import
- Allowing users to install a predefined bundle of plugins and themes with one action
- WP Crowdfunding login redirect fails
- How can we extent core/group or cover block?
- Conclude woocommerce-shipping-totals
- Uncaught ArgumentCountError
- Private WordPress Content
- Hide attributes based on previous selection
- Secure way to add JS Script to WordPress filesystem
- Customize WP Filter Hook
- Not applying update field (acf) in my plugin
- wp_add_inline_script vs wordpress fetch api
- How to use Rewrite Tag Parameters in URL-rewritten Page’s Titles (META & H1)?
- WordPress Website is very slow after migration
- Local WP site can’t locate plugins or theme
- WP galleries in posts after import. How do I do it?
- search by meta field in the regular WordPress admin panel search
- adding firstname and lastname on registration
- Plugin question, How to insert a colon in a value box
- How can I create a drawable image for a contact form
- Changing slug when post is copied
- Show the author only own types of publications. (JetEngine, Elementor)
- Change homepage logo for each site multisite setup
- Force post update with React
- Show different page depending on user role
- WooCommerce booking error: call_user_func_array() expects parameter 1 to be a valid callback
- My site has only one page, and users access my uploaded mp3 files from there – can I analyze access by song title?
- Email notification when registred user clicks a link/visits page
- Need a Category List for the category slug
- Difficult case to solve – Select + select = result(related data)
- Woocommerce attatch files and custom input meta to cart items and then to order
- How to append TinyMCE editor in TinyMCE editor’s popup
- What plugin presents these extra columns in wp-admin -> plugins
- How to disable display all posts from all categories?
- use the live preview feature in the plugin
- add to cart linking to add to cart page
- How to get wp_editor field content when it is inside a Form?
- How to filter posts with a wp query by a custom dropdown type field with the magic fields plugin
- how to override g5-ere plugins files in main theme?
- Custom Login and Password Reset for BuddyPress Website
- Adjusting Website Display at 100% Zoom
- “All plugins are updated” but they aren’t
- Add new Plugin or Delete option Not Showing and Also Theme delete option not showing
- Tickera missing attendees – No attendees found
- How can I hide Dashboard notifications for just one particular plugin?
- How to add a react element into the DOM node (only on the user-interaction like on click) without using the state?
- My english page does not show
- How can I disable links for categories but keep showing them on my blog in an Elementor Template?
- Need to add custom cart item data
- How to call a logging function from a generic utility plugin from another plguin
- Changing a class state in a wordpress plugin
- WordPress – How to add an attribute to a plugin inline script tag
- Trying to Make a Sidebar/Tab on Specific Posts
- WPForms Product URL
- Product Search by ZIp Code Radius
- View page source code – Where is the code located for PRODUCTS to edit manually
- Prevent part of plugin loading in another menu
- How to add Upload File function in WordPress Plugin
- Form submission issue in wordpress admin (custom plugin) using $_GET for searching & filtering
- Plugin recommendation needed: expiring links
- How do I edit text color for shortcode?
- call_user_func_array() expects parameter 1 to be a valid callback, class ‘Automattic\Jetpack\Extensions\Contact_Form\Contact_Form_Block’ not found
- Include theme header and footer in custom plugin
- Will latest plugin update add also previous update?
- How can I process data in parts to prevent Fatal error: Maximum execution time of 30 seconds exceeded
- wp_remote_get in scheduled event action returns wp login page
- How to manual update a private WordPress plugin from free to pro version
- Multilanguange with domaine
- WordPress “wp cron event run” as Siteground cron job
- PHP is NOT allowed in your code (disabled by your theme or a plugin)
- coauthors_plus_edit_authors to display metabox based on author caps
- Setup Wizard Permission Error on Activation (403)
- “Virtual” image in WP Media Library (external URL)
- Is there a plugin or resource for custom implementation for OAUTH and regular user login?
- comments meta box not showing in my plugin post type
- Editing a block to add a button to have a drop cap
- Disable Plugin if not logged in
- Problem with invoice its show #!trpst#trp-gettext data
- Expose environment (hosted on Docker) both on localhost and Ngrok
- WP Form Date Field – Remove Day option
- opening and closing elementor-widget-container