Title: register_block_script_module_id
Published: April 3, 2024
Last modified: February 24, 2026

---

# register_block_script_module_id( array $metadata, string $field_name, int $index ): string|false

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/register_block_script_module_id/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/register_block_script_module_id/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/register_block_script_module_id/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/register_block_script_module_id/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/register_block_script_module_id/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/register_block_script_module_id/?output_format=md#wp--skip-link--target)

Finds a script module ID for the selected block metadata field. It detects when 
a path to file was provided and optionally finds a corresponding asset file with
details necessary to register the script module under with an automatically generated
module ID. It returns unprocessed script module ID otherwise.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/register_block_script_module_id/?output_format=md#parameters)󠁿

 `$metadata`arrayrequired

Block metadata.

`$field_name`stringrequired

Field name to pick from metadata.

`$index`intoptional

Index of the script module ID to register when multiple items passed. Default 0.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/register_block_script_module_id/?output_format=md#return)󠁿

 string|false Script module ID or false on failure.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/register_block_script_module_id/?output_format=md#source)󠁿

    ```php
    function register_block_script_module_id( $metadata, $field_name, $index = 0 ) {
    	if ( empty( $metadata[ $field_name ] ) ) {
    		return false;
    	}

    	$module_id = $metadata[ $field_name ];
    	if ( is_array( $module_id ) ) {
    		if ( empty( $module_id[ $index ] ) ) {
    			return false;
    		}
    		$module_id = $module_id[ $index ];
    	}

    	$module_path = remove_block_asset_path_prefix( $module_id );
    	if ( $module_id === $module_path ) {
    		return $module_id;
    	}

    	$path                  = dirname( $metadata['file'] );
    	$module_asset_raw_path = $path . '/' . substr_replace( $module_path, '.asset.php', - strlen( '.js' ) );
    	$module_id             = generate_block_asset_handle( $metadata['name'], $field_name, $index );
    	$module_asset_path     = wp_normalize_path(
    		realpath( $module_asset_raw_path )
    	);

    	$module_path_norm = wp_normalize_path( realpath( $path . '/' . $module_path ) );
    	$module_uri       = get_block_asset_url( $module_path_norm );

    	$module_asset        = ! empty( $module_asset_path ) ? require $module_asset_path : array();
    	$module_dependencies = isset( $module_asset['dependencies'] ) ? $module_asset['dependencies'] : array();
    	$block_version       = isset( $metadata['version'] ) ? $metadata['version'] : false;
    	$module_version      = isset( $module_asset['version'] ) ? $module_asset['version'] : $block_version;

    	$supports_interactivity_true = isset( $metadata['supports']['interactivity'] ) && true === $metadata['supports']['interactivity'];
    	$is_interactive              = $supports_interactivity_true || ( isset( $metadata['supports']['interactivity']['interactive'] ) && true === $metadata['supports']['interactivity']['interactive'] );
    	$supports_client_navigation  = $supports_interactivity_true || ( isset( $metadata['supports']['interactivity']['clientNavigation'] ) && true === $metadata['supports']['interactivity']['clientNavigation'] );

    	$args = array();

    	// Blocks using the Interactivity API are server-side rendered, so they are
    	// by design not in the critical rendering path and should be deprioritized.
    	if ( $is_interactive ) {
    		$args['fetchpriority'] = 'low';
    		$args['in_footer']     = true;
    	}

    	// Blocks using the Interactivity API that support client-side navigation
    	// must be marked as such in their script modules.
    	if ( $is_interactive && $supports_client_navigation ) {
    		wp_interactivity()->add_client_navigation_support_to_script_module( $module_id );
    	}

    	wp_register_script_module(
    		$module_id,
    		$module_uri,
    		$module_dependencies,
    		$module_version,
    		$args
    	);

    	return $module_id;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/blocks.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/blocks.php#L145)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/blocks.php#L145-L206)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/register_block_script_module_id/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_interactivity()](https://developer.wordpress.org/reference/functions/wp_interactivity/)`wp-includes/interactivity-api/interactivity-api.php` |

Retrieves the main [WP_Interactivity_API](https://developer.wordpress.org/reference/classes/wp_interactivity_api/) instance.

  | 
| [wp_register_script_module()](https://developer.wordpress.org/reference/functions/wp_register_script_module/)`wp-includes/script-modules.php` |

Registers the script module if no script module with that script module identifier has already been registered.

  | 
| [get_block_asset_url()](https://developer.wordpress.org/reference/functions/get_block_asset_url/)`wp-includes/blocks.php` |

Gets the URL to a block asset.

  | 
| [generate_block_asset_handle()](https://developer.wordpress.org/reference/functions/generate_block_asset_handle/)`wp-includes/blocks.php` |

Generates the name for an asset based on the name of the block and the field name provided.

  | 
| [remove_block_asset_path_prefix()](https://developer.wordpress.org/reference/functions/remove_block_asset_path_prefix/)`wp-includes/blocks.php` |

Removes the block asset’s path prefix if provided.

  | 
| [wp_normalize_path()](https://developer.wordpress.org/reference/functions/wp_normalize_path/)`wp-includes/functions.php` |

Normalizes a filesystem path.

  |

[Show 1 more](https://developer.wordpress.org/reference/functions/register_block_script_module_id/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/register_block_script_module_id/?output_format=md#)

| Used by | Description | 
| [register_block_type_from_metadata()](https://developer.wordpress.org/reference/functions/register_block_type_from_metadata/)`wp-includes/blocks.php` |

Registers a block type from the metadata stored in the `block.json` file.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/register_block_script_module_id/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.5.0](https://developer.wordpress.org/reference/since/6.5.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fregister_block_script_module_id%2F)
before being able to contribute a note or feedback.