register_block_script_module_id()


WordPressのregister_block_script_module_id()関数は、ブロックエディター用のスクリプトモジュールIDを登録するために使用されます。この関数は、ブロックのスクリプトやスタイルを効率的に管理するために役立ちます。

構文

register_block_script_module_id( string $handle, string $src, array $deps = array(), string|bool|null $ver = false, array $args = array() );

引数の説明:

  • $handle(string) — スクリプトモジュールのハンドル名。一意である必要があります。
  • $src(string) — スクリプトモジュールのURL。
  • $deps(array) — このスクリプトモジュールが依存する他のスクリプトモジュールのハンドルの配列。
  • $ver(string|bool|null) — スクリプトモジュールのバージョン。
  • $args(array) — 追加の引数。

例1: 基本的なスクリプトモジュールの登録

この例では、シンプルなスクリプトモジュールを登録します。

register_block_script_module_id('my-script-module', get_template_directory_uri() . '/js/my-script.js');

例2: 依存関係を持つスクリプトモジュールの登録

この例では、依存関係を持つスクリプトモジュールを登録します。

register_block_script_module_id('my-dependent-script', get_template_directory_uri() . '/js/dependent-script.js', array('wp-blocks'));

例3: バージョン指定付きのスクリプトモジュールの登録

この例では、バージョン指定付きのスクリプトモジュールを登録します。

register_block_script_module_id('my-versioned-script', get_template_directory_uri() . '/js/versioned-script.js', array(), '1.0.0');

例4: 追加引数を使用したスクリプトモジュールの登録

この例では、追加引数を使用してスクリプトモジュールを登録します。

register_block_script_module_id('my-args-script', get_template_directory_uri() . '/js/args-script.js', array(), false, array('in_footer' => true));

例5: 複数のスクリプトモジュールを一度に登録

この例では、複数のスクリプトモジュールを一度に登録します。

register_block_script_module_id('script-one', get_template_directory_uri() . '/js/script-one.js');register_block_script_module_id('script-two', get_template_directory_uri() . '/js/script-two.js');

例6: 条件付きでスクリプトモジュールを登録

この例では、条件付きでスクリプトモジュールを登録します。

if(is_page()){register_block_script_module_id('page-script', get_template_directory_uri() . '/js/page-script.js');}

例7: スクリプトモジュールをテーマに登録

この例では、テーマ内でスクリプトモジュールを登録します。

add_action('init', function(){register_block_script_module_id('theme-script', get_template_directory_uri() . '/js/theme-script.js');});

例8: プラグイン内でスクリプトモジュールを登録

この例では、プラグイン内でスクリプトモジュールを登録します。

add_action('init', function(){register_block_script_module_id('plugin-script', plugins_url('js/plugin-script.js', __FILE__));});

注意点

  • スクリプトモジュールのハンドル名は一意である必要があります。
  • 依存関係を正しく設定しないと、スクリプトが正しく動作しない場合があります。
  • バージョン管理を使用することで、キャッシュの問題を回避できます。

関連機能: