Title: Using wp_localize_script for a block script
Last modified: May 16, 2023

---

# Using wp_localize_script for a block script

 *  Resolved [little_bird](https://wordpress.org/support/users/little_bird/)
 * (@little_bird)
 * [3 years ago](https://wordpress.org/support/topic/using-wp_localize_script-for-a-block-script/)
 * Hi,
 * I am developing a Gutenberg block using the [@wordpress](https://wordpress.org/support/users/wordpress/)/
   create-block tool. In the block.json I added a frontend script through
 *     ```wp-block-code
       "viewScript": [
           "file:./hwscript.js",
           "hbw-words-script"
       ],
       ```
   
 * Since register_block_type registers and enqueues the script using the data from
   block.json, how can I use wp_localize_script without enqueuing again the hwscript.
   js file?

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [3 years ago](https://wordpress.org/support/topic/using-wp_localize_script-for-a-block-script/#post-16746670)
 * When we enqueue a script, the resulting HTML link tag’s ID attribute is the script’s
   registration handle with “-js” appended. Look through a page’s source HTML code
   for something similar for your hwscript.js file. It may not be the same for block
   scripts, but if it is, you can deduce the original registration handle.
 * Rather hacky, but you could bypass the usual localization process and directly
   output the script assignments you need within an inline `<script>` block from
   the ‘wp_print_scripts’ action hook.
 * Disclaimer: there may be a “right” way to do this that I’m unaware of, but one
   of these two should work for you, if not entirely “proper”.
 *  Thread Starter [little_bird](https://wordpress.org/support/users/little_bird/)
 * (@little_bird)
 * [3 years ago](https://wordpress.org/support/topic/using-wp_localize_script-for-a-block-script/#post-16748099)
 * Hi bcworkz,
 * You are right, I found the handle through the id attribute of the HTML tag. I
   was downright convinced that the value in the second element of the “viewScript”
   array was the handle of the script, so what is supposed to be that string?
 * Thanks
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [3 years ago](https://wordpress.org/support/topic/using-wp_localize_script-for-a-block-script/#post-16749067)
 * Good question! I have no idea, sorry. After a bit of research, I think it’s an
   alternative way of referencing a script that is already registered. With
 *     ```
       "viewScript": [
           "file:./hwscript.js",
           "hbw-words-script"
       ],
       ```
   
 * you’re saying enqueue both hwscript.js and the script registered as “hbw-words-
   script”. It’s not a handle for your file, it’s a handle of some other file to
   be enqueued as well. If such a registration does not exist, it’s probably ignored.
 * I decided this is the case from [https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#wpdefinedasset](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#wpdefinedasset)
 *  Thread Starter [little_bird](https://wordpress.org/support/users/little_bird/)
 * (@little_bird)
 * [3 years ago](https://wordpress.org/support/topic/using-wp_localize_script-for-a-block-script/#post-16753309)
 * I think you are right. Anyway, the documentation is not clear about this. I managed
   to use wp_localize_script with the handle I found on the HTML script tag.
 * Thanks
 *  [james0r](https://wordpress.org/support/users/james0r/)
 * (@james0r)
 * [2 years, 10 months ago](https://wordpress.org/support/topic/using-wp_localize_script-for-a-block-script/#post-16923348)
 * Thanks [@bcworkz](https://wordpress.org/support/users/bcworkz/). I know it’s 
   not recommended but I went with
 *     ```wp-block-code
           if (!empty($this->data)) {
             wp_localize_script('blocknamespace-' . $this->name. '-editor-script', $this->name, $this->data);
           }
       ```
   
 * And it works like a charm.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Using wp_localize_script for a block script’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 5 replies
 * 3 participants
 * Last reply from: [james0r](https://wordpress.org/support/users/james0r/)
 * Last activity: [2 years, 10 months ago](https://wordpress.org/support/topic/using-wp_localize_script-for-a-block-script/#post-16923348)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
