wp_enqueue_script

説明

void wp_enqueue_script( string $handle [ , string $src = false [ , array $deps = array() [ , string $ver = false [ , bool $in_footer = falsearray $args = array() ] ] ] ] )
スクリプト(JavaScript)の重複出力を回避するために、使用するスクリプトを出力用のキューに入れる。パラメータ$srcが指定された場合は、グローバル変数$wp_scriptsに登録してから出力用のキューに入れる。

パラメータ

  • $handle
    スクリプトのハンドル名を指定。
  • $src
    スクリプトのパス名を指定(省略時はfalse)。
  • $deps
    関連するスクリプトのハンドル名を配列で指定(省略時はarray())。
  • $var
    スクリプトのバージョンを指定(省略時はfalse)。
  • $in_footer$args
    wp_footerで出力する場合はtrueを指定(省略時はfalse)。[6.3.0]'strategy'、'in_footer'キーを持つ連想配列を指定(省略時はarray())

返り値

返り値はありません。

注意

このwp_enqueue_script関数の呼び出しは、原則的にinitアクションの実行後に行うことになり、テーマで表示するページ向けなら'wp_enqueue_scripts'アクションを、管理ページ向けなら'admin_enqueue_scripts'アクションを利用する。キューに入れたスクリプトは wp_head関数または wp_footer関数(実際はその中のアクション)によって出力される。

スクリプトはグローバル変数$wp_scriptsのプロパティregisteredに連想配列として登録される。デフォルトで登録されているスクリプトは次の通りである(3.3時点)。

[5.2.0]'clipboard'が追加された。

[6.3.0]第5パラメータがbool型から連想配列に変更になった。連想配列は'strategy'と'in_footer'が有効であり、'strategy'の値は'defer'または'async'が、'in_footer'はtrueまたはfalseを指定できる。第5パラメータが配列以外の場合、その値はboolでキャストされ、キー'in_footer'の値となり、互換性が維持されている。
ハンドル名(handle)パス名(src)関連するスクリプト(deps)バージョン(ver)出力関数
utils/wp-admin/js/utils.js-20101110wp_head
common/wp-admin/js/common.jsjquery, hoverIntent, utils20111130wp_head, wp_footer
sack/wp-includes/js/tw-sack.js-1.6.1wp_footer
quicktags/wp-includes/js/quicktags.js-20111114wp_footer
colorpicker/wp-includes/js/colorpicker.jsprototype3517mwp_head
editor/wp-admin/js/editor.jsutils, jquery20111117wp_head, wp_footer
clipboard/wp-admin/js/clipboard.jsutils, jquerywp_head, wp_footer
wp-fullscreen/wp-admin/js/wp-fullscreen.jsjquery20111116wp_head, wp_footer
prototype/wp-includes/js/prototype.js-1.6.1wp_head
wp-ajax-response/wp-includes/js/wp-ajax-response.jsjquery20091119wp_head, wp_footer
wp-pointer/wp-includes/js/wp-pointer.jsjquery-ui-widget, jquery-ui-position20111129awp_head, wp_footer
autosave/wp-includes/js/autosave.jsschedule, wp-ajax-response20111129wp_head, wp_footer
wp-lists/wp-includes/js/wp-lists.jswp-ajax-response20110521wp_head, wp_footer
scriptaculous-root/wp-includes/js/scriptaculous/wp-scriptaculous.jsprototype1.8.3wp_head
scriptaculous-builder/wp-includes/js/scriptaculous/builder.jsscriptaculous-root1.8.3wp_head
scriptaculous-dragdrop/wp-includes/js/scriptaculous/dragdrop.jsscriptaculous-builder, scriptaculous-effects1.8.3wp_head
scriptaculous-effects/wp-includes/js/scriptaculous/effects.jsscriptaculous-root1.8.3wp_head
scriptaculous-slider/wp-includes/js/scriptaculous/slider.jsscriptaculous-effects1.8.3wp_head
scriptaculous-sound/wp-includes/js/scriptaculous/sound.jsscriptaculous-root1.8.3wp_head
scriptaculous-controls/wp-includes/js/scriptaculous/controls.jsscriptaculous-root1.8.3wp_head
scriptaculous-scriptaculous-dragdrop, scriptaculous-slider, scriptaculous-controls1.8.3wp_head
cropper/wp-includes/js/crop/cropper.jsscriptaculous-dragdrop20070118wp_head
jquery/wp-includes/js/jquery/jquery.js-1.7.1wp_head
jquery-ui-core/wp-includes/js/jquery/ui/jquery.ui.core.min.jsjquery1.8.16wp_head, wp_footer
jquery-effects-core/wp-includes/js/jquery/ui/jquery.effects.core.min.jsjquery1.8.16wp_head, wp_footer
jquery-effects-blind/wp-includes/js/jquery/ui/jquery.effects.blind.min.jsjquery-effects-core1.8.16wp_head, wp_footer
jquery-effects-bounce/wp-includes/js/jquery/ui/jquery.effects.bounce.min.jsjquery-effects-core1.8.16wp_head, wp_footer
jquery-effects-clip/wp-includes/js/jquery/ui/jquery.effects.clip.min.jsjquery-effects-core1.8.16wp_head, wp_footer
jquery-effects-drop/wp-includes/js/jquery/ui/jquery.effects.drop.min.jsjquery-effects-core1.8.16wp_head, wp_footer
jquery-effects-explode/wp-includes/js/jquery/ui/jquery.effects.explode.min.jsjquery-effects-core1.8.16wp_head, wp_footer
jquery-effects-fade/wp-includes/js/jquery/ui/jquery.effects.fade.min.jsjquery-effects-core1.8.16wp_head, wp_footer
jquery-effects-fold/wp-includes/js/jquery/ui/jquery.effects.fold.min.jsjquery-effects-core1.8.16wp_head, wp_footer
jquery-effects-highlight/wp-includes/js/jquery/ui/jquery.effects.highlight.min.jsjquery-effects-core1.8.16wp_head, wp_footer
jquery-effects-pulsate/wp-includes/js/jquery/ui/jquery.effects.pulsate.min.jsjquery-effects-core1.8.16wp_head, wp_footer
jquery-effects-scale/wp-includes/js/jquery/ui/jquery.effects.scale.min.jsjquery-effects-core1.8.16wp_head, wp_footer
jquery-effects-shake/wp-includes/js/jquery/ui/jquery.effects.shake.min.jsjquery-effects-core1.8.16wp_head, wp_footer
jquery-effects-slide/wp-includes/js/jquery/ui/jquery.effects.slide.min.jsjquery-effects-core1.8.16wp_head, wp_footer
jquery-effects-transfer/wp-includes/js/jquery/ui/jquery.effects.transfer.min.jsjquery-effects-core1.8.16wp_head, wp_footer
jquery-ui-accordion/wp-includes/js/jquery/ui/jquery.ui.accordion.min.jsjquery-ui-core, jquery-ui-widget1.8.16wp_head, wp_footer
jquery-ui-autocomplete/wp-includes/js/jquery/ui/jquery.ui.autocomplete.min.jsjquery-ui-core, jquery-ui-widget, jquery-ui-position1.8.16wp_head, wp_footer
jquery-ui-button/wp-includes/js/jquery/ui/jquery.ui.button.min.jsjquery-ui-core, jquery-ui-widget1.8.16wp_head, wp_footer
jquery-ui-datepicker/wp-includes/js/jquery/ui/jquery.ui.datepicker.min.jsjquery-ui-core1.8.16wp_head, wp_footer
jquery-ui-dialog/wp-includes/js/jquery/ui/jquery.ui.dialog.min.jsjquery-ui-resizable, jquery-ui-draggable, jquery-ui-button, jquery-ui-position1.8.16wp_head, wp_footer
jquery-ui-draggable/wp-includes/js/jquery/ui/jquery.ui.draggable.min.jsjquery-ui-core, jquery-ui-mouse1.8.16wp_head, wp_footer
jquery-ui-droppable/wp-includes/js/jquery/ui/jquery.ui.droppable.min.jsjquery-ui-draggable1.8.16wp_head, wp_footer
jquery-ui-mouse/wp-includes/js/jquery/ui/jquery.ui.mouse.min.jsjquery-ui-widget1.8.16wp_head, wp_footer
jquery-ui-position/wp-includes/js/jquery/ui/jquery.ui.position.min.jsjquery1.8.16wp_head, wp_footer
jquery-ui-progressbar/wp-includes/js/jquery/ui/jquery.ui.progressbar.min.jsjquery-ui-widget1.8.16wp_head, wp_footer
jquery-ui-resizable/wp-includes/js/jquery/ui/jquery.ui.resizable.min.jsjquery-ui-core, jquery-ui-mouse1.8.16wp_head, wp_footer
jquery-ui-selectable/wp-includes/js/jquery/ui/jquery.ui.selectable.min.jsjquery-ui-core, jquery-ui-mouse1.8.16wp_head, wp_footer
jquery-ui-slider/wp-includes/js/jquery/ui/jquery.ui.slider.min.jsjquery-ui-core, jquery-ui-mouse1.8.16wp_head, wp_footer
jquery-ui-sortable/wp-includes/js/jquery/ui/jquery.ui.sortable.min.jsjquery-ui-core, jquery-ui-mouse1.8.16wp_head, wp_footer
jquery-ui-tabs/wp-includes/js/jquery/ui/jquery.ui.tabs.min.jsjquery-ui-core, jquery-ui-widget1.8.16wp_head, wp_footer
jquery-ui-widget/wp-includes/js/jquery/ui/jquery.ui.widget.min.jsjquery1.8.16wp_head, wp_footer
jquery-form/wp-includes/js/jquery/jquery.form.jsjquery2.73wp_head, wp_footer
jquery-color/wp-includes/js/jquery/jquery.color.jsjquery2.0-4561mwp_head, wp_footer
suggest/wp-includes/js/jquery/suggest.jsjquery1.1-20110113wp_head, wp_footer
schedule/wp-includes/js/jquery/jquery.schedule.jsjquery20mwp_head, wp_footer
jquery-query/wp-includes/js/jquery/jquery.query.jsjquery2.1.7wp_head, wp_footer
jquery-serialize-object/wp-includes/js/jquery/jquery.serialize-object.jsjquery0.2wp_head, wp_footer
jquery-hotkeys/wp-includes/js/jquery/jquery.hotkeys.jsjquery0.0.2mwp_head, wp_footer
jquery-table-hotkeys/wp-includes/js/jquery/jquery.table-hotkeys.jsjquery, jquery-hotkeys20090102wp_head, wp_footer
thickbox/wp-includes/js/thickbox/thickbox.jsjquery3.1-20111117wp_head, wp_footer
jcrop/wp-includes/js/jcrop/jquery.Jcrop.jsjquery0.9.8-20110113wp_head, wp_footer
swfobject/wp-includes/js/swfobject.js-2.2wp_head
plupload/wp-includes/js/plupload/plupload.js-1511-20111112wp_head
plupload-html5/wp-includes/js/plupload/plupload.html5.jsplupload1511-20111112wp_head
plupload-flash/wp-includes/js/plupload/plupload.flash.jsplupload1511-20111112wp_head
plupload-silverlight/wp-includes/js/plupload/plupload.silverlight.jsplupload1511-20111112wp_head
plupload-html4/wp-includes/js/plupload/plupload.html4.jsplupload1511-20111112wp_head
plupload-all-plupload, plupload-html5, plupload-flash, plupload-silverlight, plupload-html41511-20111112wp_head
plupload-handlers/wp-includes/js/plupload/handlers.jsplupload-all, jquery20111120wp_head
swfupload/wp-includes/js/swfupload/swfupload.js-2201-20110113wp_head
swfupload-swfobject/wp-includes/js/swfupload/plugins/swfupload.swfobject.jsswfupload, swfobject2201awp_head
swfupload-queue/wp-includes/js/swfupload/plugins/swfupload.queue.jsswfupload2201wp_head
swfupload-speed/wp-includes/js/swfupload/plugins/swfupload.speed.jsswfupload2201wp_head
swfupload-all/wp-includes/js/swfupload/swfupload-all.js-2201awp_head
swfupload-handlers/wp-includes/js/swfupload/handlers.jsswfupload-all, jquery2201-20110524wp_head
comment-reply/wp-includes/js/comment-reply.js-20090102wp_head
json2/wp-includes/js/json2.js-2011-02-23wp_head
imgareaselect/wp-includes/js/imgareaselect/jquery.imgareaselect.jsjquery0.9.6-20110515wp_head, wp_footer
password-strength-meter/wp-admin/js/password-strength-meter.jsjquery20101027wp_head, wp_footer
user-profile/wp-admin/js/user-profile.jsjquery, password-strength-meter20110628wp_head, wp_footer
admin-bar/wp-includes/js/admin-bar.js-20111130wp_footer
wplink/wp-includes/js/wplink.jsjquery, wpdialogs20111128wp_head, wp_footer
wpdialogs/wp-includes/js/tinymce/plugins/wpdialogs/js/wpdialog.jsjquery-ui-dialog20110528wp_head, wp_footer
wpdialogs-popup/wp-includes/js/tinymce/plugins/wpdialogs/js/popup.jswpdialogs20110421wp_head, wp_footer
word-count/wp-admin/js/word-count.jsjquery20110515wp_head, wp_footer
media-upload/wp-admin/js/media-upload.jsthickbox20110930wp_head, wp_footer
hoverIntent/wp-includes/js/hoverIntent.jsjquery20090102wp_head, wp_footer

「関連するスクリプト(deps)」のハンドル名のスクリプトは、それらがwp_enqueue_script関数にて指定されていない場合でも自動的に出力されるものになる。例えば、wp_enqueue_script( 'jquery-ui-tabs' )を指定した場合は、次のようにwp-head関数で'jquery'が、wp-footer関数で'jquery-ui-core'、'jquery-ui-widget'、'jquery-ui-tabs'のスクリプトが出力される。
<head>
<script type='text/javascript' src='http://localhost/wp/wp-includes/js/jquery/jquery.js?ver=1.7.1'></script>
</head>
<body class="home">

<!-- 中略 -->

<footer>
<script type='text/javascript' src='http://localhost/wp/wp-includes/js/jquery/ui/jquery.ui.core.min.js?ver=1.8.16'></script>
<script type='text/javascript' src='http://localhost/wp/wp-includes/js/jquery/ui/jquery.ui.widget.min.js?ver=1.8.16'></script>
<script type='text/javascript' src='http://localhost/wp/wp-includes/js/jquery/ui/jquery.ui.tabs.min.js?ver=1.8.16'></script>
</footer>
</body>

使用例

  • jQueryをキューに入れる。
    <?php wp_enqueue_script( 'jquery' ); ?>
  • テーマ独自スクリプト'mytheme-js'を$wp_scriptsに登録後、キューに入れる。
    <?php wp_enqueue_script( 'mytheme-js', get_template_directory_uri().'/js/mytheme.js' ); ?>
  • テーマ独自スクリプト'mytheme-js'とjQueryをキューに入れる。
    <?php wp_enqueue_script( 'mytheme-js', get_template_directory_uri().'/js/mytheme.js', array( 'jquery' ) ); ?>
  • テーマ独自スクリプト'mytheme-js'を$wp_scriptsに登録後、フッター出力としてキューに入れる。
    <?php wp_enqueue_script( 'mytheme-js', get_template_directory_uri().'/js/mytheme.js', array(), false, true ); ?>

フィルター

実行するフィルターはありません。

アクション

実行するアクションはありません。

ソースファイル

/wp-includes/functions.wp-scripts.php

最終更新 : 2023年08月09日 14:21



お勧め

Login rebuilder 2.3.0の新機能を紹介(2018年5月27日 更新)

プラグイン「Login rebuilder」はログイン時の履歴(ログ)や不正なログインページへのアクセス履歴を保存している。これらの履歴情報はダッシュボートで確認できるが、バージョン2.3.0においてそれぞれがダウンロードできるようになった。

古めのリビジョンを削除した時のメモ(2022年5月16日 更新)

大きく育ったデータベースを整理するため、古めのリビジョンとそれに紐づいたカスタムフィールドデータを削除。と思ったら「The total number of locks exceeds the lock table size」というエラーが発生して削除できなかった。

決まった値を返すだけの関数群(2018年5月27日 更新)

WordPressのソースコードを眺めているとフィルター関数名で'__return_○○'が指定されている部分を時々見かけます。それらの関数定義を見てみると、すべて決まった値を単純に返すだけでした。これらの関数がちょっと気になったので、まとめてみました。

「Align widgets horizontally」プラグインのフィルター使用例(2024年5月15日 更新)

ダッシュボードのウィジェットを横並びに変える「Align widgets horizontally」プラグインには緒設定を変更できるフィルターが用意されている。ここではフィルターの使い方と設定できる項目について紹介する。

WP_Postオブジェクトで投稿情報を直感的に使う(2012年10月4日 更新)

WordPress 3.5で登場するであろう WP_Postクラス。WordPressの投稿情報は、投稿情報本体のほか、カテゴリーや投稿タグ、カスタムフィールドなど、いくつかに分散して記録され、それぞれの情報を取得する際は、いくつかの関数をしていた。新しいWP_Postクラスにより、その習慣とおさらばできるかもしれない。