wp_deregister_script_module()


WordPressのwp_deregister_script_module()関数は、登録されたスクリプトモジュールを解除するために使用されます。

構文

wp_deregister_script_module( string $module_id );
  • $module_id(string) — 解除するスクリプトモジュールのID。

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

登録されたスクリプトモジュールを解除する基本的な例です。

wp_deregister_script_module('my-module');

例2: 条件付きでスクリプトモジュールを解除

特定の条件下でのみスクリプトモジュールを解除する例です。

if (is_page('about')) { wp_deregister_script_module('about-module'); }

例3: 複数のスクリプトモジュールを解除

複数のスクリプトモジュールを一度に解除する例です。

wp_deregister_script_module('module-1'); wp_deregister_script_module('module-2');

例4: フックを使用してスクリプトモジュールを解除

wp_enqueue_scriptsフックを使用してスクリプトモジュールを解除する例です。

add_action('wp_enqueue_scripts', function() { wp_deregister_script_module('unwanted-module'); });

例5: 管理画面でのスクリプトモジュール解除

管理画面でのみスクリプトモジュールを解除する例です。

add_action('admin_enqueue_scripts', function() { wp_deregister_script_module('admin-module'); });

例6: 特定の投稿タイプでスクリプトモジュールを解除

特定の投稿タイプでのみスクリプトモジュールを解除する例です。

if (get_post_type() === 'book') { wp_deregister_script_module('book-module'); }

例7: スクリプトモジュールを解除して代替を登録

スクリプトモジュールを解除し、代替モジュールを登録する例です。

wp_deregister_script_module('old-module'); wp_register_script_module('new-module', get_template_directory_uri() . '/js/new-module.js');

例8: プラグインでスクリプトモジュールを解除

プラグインが登録したスクリプトモジュールを解除する例です。

add_action('wp_enqueue_scripts', function() { wp_deregister_script_module('plugin-module'); }, 20);

注意点

  • スクリプトモジュールを解除する際は、依存関係に注意してください。
  • 解除したモジュールが他のスクリプトに依存している場合、エラーが発生する可能性があります。

関連機能: