You have to make sure your script is registered with wp_register_script
before your call to wp_localize_script
.
So in your functions.php file:
add_action('init',’my demo_function');
function my demo_function() {
// Register the script
wp_register_script( 'myuserscript', 'path/to/myscript.js' );
// Localize the script with your data
$theid=get_current_user_id();
$params = array(
'userid' => $theid
);
wp_localize_script( 'myuserscript', 'MyUserParams', $params );
// Enqueued script with localized data.
wp_enqueue_script('myuserscript');
}
Then the parameter would be available to you in the frontend
<script>
var myData = MyUserParams.userid;
alert("your id is: " + myData)
</script>
Reference: wp_localize_script
Related Posts:
- How to properly dequeue scripts and styles in child theme?
- enqueue and localize script in footer
- wp_enqueue_script with dependencies doesn’t work
- Help with enqueing scripts in footer after init action
- How to cancel `wp_print_scripts`?
- Localizing script to pass a value to a javascript function
- When should I use wp_register_script() with wp_enqueue_script() vs just wp_enqueue_script()?
- Check if a script/style was enqueued/registered
- Is it possible to use wp_localize_script to create global JS variables without a specific script handle?
- How do I get the $handle for all enqueued scripts?
- How can I get a list of all enqueued scripts and styles?
- Correct Method to run scripts with dependencies without enqueue?
- Cannot deregister a script using wp_deregister_script
- How to dequeue / deregister any theme styles and scripts
- Hyphens vs. periods in the script slug in wp_register_script?
- How to load JQuery easing script in wordpress?
- wp_register_script multiple identifiers?
- How to Add a Custom Script to Customize.php
- Problem in wp_localize_script
- wp_enqueue_script vs. wp_register_script
- Child Theme – what is the scope of overwriting files?
- Best way to enqueue extremely popular scripts like bootstrap and font awesome
- How to remove some external js files from source?
- Best spot for wp_register_script() and wp_register_style()
- Select2 in WordPress
- Register script/style: Is it possible to customize the version query string via plugin?
- How to load script-related styles automatically?
- Modernizr check first, then move on to wp_register_script()/wp_enqueue_script
- How do I enqueue a JavaScript in my footer via the functions.php file?
- Including style.css in Child Theme
- getting a js file for one page
- wp_add_inline_script not adding when script_loader_tag filtered
- Retrieve URL of Script/Style and Dependencies
- Can’t see why my scripts aren’t loading when I register and enqueue them
- Including files in Child Themes
- Why won’t my scripts load?
- WordPress script file version numbers changing in live environment
- Is there a way to list all the JavaScript scripts that are actually loaded by WordPress? Do we care?
- Change script type and src of plugins in theme
- How to enqueue JavaScript file that depends on CSS stylesheet
- Styles and scripts inside template part
- wp_dequeue_style and wp_dequeue_script not working on server but does work on local xampp
- Adding multiple wp_register_scripts for templates in WordPres?
- How to enqueue the script without hardcoded in the theme files?
- why quotes shown in WordPress?
- Enqueuing script is adding extra text for google maps [closed]
- Deregister multiple scripts using a function?
- Enqueuing a script before anything else
- wp_enqueue_script was called incorrectly Issue when active plugins of WordPress Admin Bar Improved
- wp_enqueue_scripts does not work
- Modular CSS and JS in Child Themes
- If a Script has been enqueued but not registered can I still removed it?
- include jquery plugin file not working
- How to use wp_register_script / wp_enqueue_script for multiple queries
- How to register script with null value for version?
- Using wp_enqueue_script on scripts that contain PHP
- Calling wp_localize_script in a different function
- is there a way to get all queued scripts/styles into a template without `get_header()`?
- Stop a plugins js and css from loading on all pages and show up only where it is called
- How do I pass $in_foooter to wp_enqueue_script() without passing other params?
- Get localize of a loaded javascript
- Enqueue registered dependencies only when necessary
- Proper use of wp_localize_script?
- How do I display the handle for all front-end enqueued scripts in plugin options page?
- wp_enqueue doesn’t load dependencies
- Why do WordPress adds the id=”handle-{js|css}” attribute to scripts and stylesheet?
- Dequeue/deregister scripts for everybody but the administrator
- wp_enqueue_scripts, wp_register_scripts, wp_print_scripts: i’m confused
- wp_localize_script with boolean and init
- Hook for writing text string after footer scripts
- Benefits to enqueuing site scripts/stylesheets?
- How do I load custom scripts and styles for a page?
- Enqueued scripts and styles loading in WordPress Dashboard as well
- How can I load script and style in specific page in the back-end?
- Enqueue script with url_query variables?
- How to properly add Bootstrap and JQuery Javascripts?
- Using multiple versions of jQuery while still calling it like WP likes
- Enqueue Style for a page/pages only
- debugging js scripts
- Enqueue never runs
- Unable to dequeue a plugin script. I think I’ve tried everything
- Action hooked on wp_enqueue_scripts running twice
- wp_enqueue_scripts wont load my scripts
- JavaScript enque stopped working
- Use of CONSTANT in wp_enqueue_script not possible?
- wp_enqueue_scripts are apearing inline rather than as links
- Load same stylesheet for front end and wp-admin
- How to remove all enqueued assets from the active theme?
- Should we explicitely enqueue jQuery and our theme’s main stylesheet?
- Enqueueing Scripts/Styles mid-page
- Javascript asset not enqueuing with the rest
- load jquery in footer not working
- Check for Twitter Bootstrap Loaded
- wp_enqueue_script was called incorrectly. Scripts and styles
- Unqueue css file from specific page
- Pass post ID to JavaScript outside the loop
- True parameter but jquery register in header and not in the footer with wp_register_script
- Bootstrap css not getting applied to the custom WordPress Dashboard Widget
- WooCommerce not enqueuing its scripts [closed]
- See Scripts used by Page