I came to this question almost 3 years late, but there is a better way of doing that now: WordPress recommends using wp_add_inline_script
when you want to pass data between PHP and JS, you can read about it here
Basically, what you will do, is changing your wp_localize_script
function call in your code, for this:
wp_add_inline_script('my-script-name', 'const my_plugin_max_length=" . get_option( "my_plugin_maxlength' ), 'before');
And then you can use the variable my_plugin_max_length
in your JS file.
Related Posts:
- wp_localize_script with boolean and init
- Pass global variable data to localize_script
- Define a global variable in a JavaScript function
- How do you check if a variable is an array in JavaScript?
- How can I determine if a variable is ‘undefined’ or ‘null’?
- Static variables in JavaScript
- What does ${} (dollar sign and curly braces) mean in a string in Javascript?
- Declaring multiple variables in JavaScript
- How can I check if an element exists in the visible DOM?
- How to interpolate variables in strings in JavaScript, without concatenation?
- How do I use this JavaScript variable in HTML?
- JavaScript check if variable exists (is defined/initialized)
- Multiple left-hand assignment with JavaScript
- Trigger Javascript on Gutenberg (Block Editor) Save
- How to intercept already localized scripts
- Gutenberg – remove / add blocks with custom script
- What are all the query parameters for getEntityRecords?
- How to use wp.hooks.addAction() in React JS/Gutenberg?
- How to only enqueue block javascript on the frontend when its needed [duplicate]
- Extend core block in Gutenberg
- Show control conditionally in Gutenberg
- Gutenberg custom block plugin with custom image sizes
- Get loading state of wp data selector
- How to get the ToggleControl Gutenberg component working for a PHP Block
- Is there a core Sortable component in Gutenberg?
- Hooking a callback into the code within a Gutenberg block
- conditional layout based on if Innerblocks is not empty
- Gutenberg extend blocks add new class name
- initial open/toggle PluginDocumentSettingPanel panel in document setting block editor gutenberg
- Is there an equivalent of the PHP function sanitize_key in Gutenberg?
- How to add extra attributes to the script tag added via wp_localize_script()
- Correctly handling WordPress core data retrieval in Gutenberg
- Is there a client side API for handling transients or options?
- What do the args for Gutenberg subpackage “hooks” function “doAction” mean?
- How to overwrite JS variable with wp_localize _script?
- How do I fire a snackbar notice in admin?
- Gutenberg Modify core taxonomy panel element via wp.hooks.addFilter
- What is “open()” in MediaUpload?
- Why is this gutenberg custom format button only available from the toolbar as a dropdown?
- Adding a text element in between title and blocks container
- Gutenberg RichText
- Provide specific example for block variation
- Use useSelect/useDispatch instead of withSelect/withDispatch
- How to handle Gutenberg wp.data async errors?
- Set current tab on a Gutenberg TabPanel component from outside that component
- Block editor: How to check if block editor has initialized and populated the data store?
- How to use wp.hooks.addAction() in React JS/Gutenberg?
- Load script after block is inserted
- Gutenberg block “This block appears to have been modified externally” on save
- Remove type attribute from script added by wp_localize_script
- How to get value of selected page template in Gutenberg editor?
- Does wp_localize_script call a Javascript Function or Simply Pass PHP Parameters to It
- How do I access site and block editor state data and use `useSelect()` or `withSelect()` to bind it to my components?
- @wordpress/components Button variants not styled
- Block Editor: How to get title and post content within the WordPress admin UI in Javascript?
- Custom block SelectControl works but equivalent ComboboxControl gives errors – where’s my mistake?
- Gutenberg passing block attributes to component in ES6/ESNext
- Can the index.asset.php file be used with the enqueue_block_editor_assets action?
- How can I add Block Style support to the core HTML block in Gutenberg?
- var is undefined in a Gutenberg block
- Gutenberg add extra attributes to custom format
- Using wp.data.select get actual tags (not id’s) used in post
- event/callback on block update?
- can a buttongroup have a label?
- How to remove p / br elements from gutenbergs editor
- Using wp_localize_script inside template page to transfer var from php to js
- Script Localization doesn’t work
- How to disable inline css styles generated by Gutenberg editor?
- Programatically update posts in database from one block to another using transform
- Gutenberg getMedia() in post query doesn’t return all featured images
- InnerBlocks restricting does not work on nested blocks
- How to transform a legacy widget into a block
- Gutenberg: Block validation Failed Richtext undefined
- Add A Tag To A Post Through JavaScript In Gutenberg UI?
- Gutenberg select categories
- Add custom HTML markup to Gutenberg RichText
- Listening change event of taxonomy term checkboxes in Gutenberg editor
- Enqueue scripts all over but not in single.php
- How to return div with custom attributes(Coding a Gutenberg Block)
- Gutenberg get core data – search
- What is the correct way to import the blocks-editor?
- Open MediaUpload from external component in Gutenberg
- Get Block Attributes in JSON REST API
- Open Featured Image Modal in WordPress Gutenberg onClick of a button
- Gutenberg extend blocks add new class name
- How to add classes and events to image in javascript using Gutenberg?
- Checks when fetching data from multiple REST API endpoints in Gutenberg
- how to localized scripts of new page requested by ajax?
- Path to image in js with wp_localize_script [closed]
- Search for a keyword across post types in a Gutenberg component
- Using apiFetch for retrieving post data in Gutenberg
- Add custom classes for blocks in editor based on custom attributes
- Looping over wordpress meta to create “ ‘s?
- How to display post content in the block editor
- How to load an additional script for a block in the block editor?
- useSelect second parameter
- How can I allow HTML in Gutenberg UI elements help text?
- what is the purpose of the namespace argument when customizing blocks?
- Gutenberg consume wp-json data and reflect in frontend the content
- in jquery read a multidimensional array via wp_localize_script()