wp_get_script_polyfill()


WordPressのwp_get_script_polyfill()関数は、古いブラウザでJavaScriptの互換性を確保するためのポリフィルスクリプトを取得するために使用されます。

構文

wp_get_script_polyfill( string|array $handles, string $feature, string $conditional );

引数の説明:

  • $handles(string|array) — 登録済みスクリプトのハンドル
  • $feature(string) — ポリフィルが必要な機能(例: ‘Promise’)
  • $conditional(string) — 条件付きコメント(例: ‘lt IE 9’)

例1: Promiseポリフィルの追加

Promise機能のポリフィルをIE用に追加します。

wp_get_script_polyfill( 'wp-polyfill', 'Promise', 'lt IE 11' );

例2: 複数ハンドルの指定

複数のスクリプトハンドルを配列で指定します。

wp_get_script_polyfill( array('polyfill-1','polyfill-2'), 'fetch', 'lt IE 10' );

例3: fetch APIのポリフィル

fetch APIの互換性を確保します。

wp_get_script_polyfill( 'whatwg-fetch', 'fetch', 'lt IE 11' );

例4: カスタム条件付きコメント

特定のブラウザバージョン向けに条件を設定します。

wp_get_script_polyfill( 'custom-polyfill', 'Array.prototype.includes', 'lt IE 12' );

例5: 複数機能のポリフィル

複数の機能に対してポリフィルを適用します。

wp_get_script_polyfill( 'wp-polyfill', 'Promise,fetch', 'lt IE 11' );

例6: テーマ内での使用

テーマのfunctions.phpでポリフィルを登録します。

add_action('wp_enqueue_scripts',function(){wp_get_script_polyfill('theme-polyfill','Object.assign','lt IE 11');});

例7: プラグイン開発での使用

プラグイン開発時にポリフィルを追加します。

wp_get_script_polyfill('plugin-polyfill','requestAnimationFrame','lt IE 10');

注意事項

  • この関数はフロントエンドでのみ有効です
  • 条件付きコメントはIE限定の機能です
  • 過度なポリフィルの使用はパフォーマンスに影響する可能性があります

関連機能: