wp_enqueue_script

説明

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

パラメータ

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

返り値

返り値はありません。

注意

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

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

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

ハンドル名(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

最終更新 : 2019年05月29日 14:37



お勧め

Contact Form 7に確認フェーズを(2012年12月16日 更新)

Contact Form 7はWordPressによるサイト製作でお世話になることが多いプラグインの1つ。ただ、クライアント様によっては、入力、確認、送信の3フェーズにしたいという要望があり、そんな時は別の方法を採用していたのですが、JavaScript(jQuery)を使ってページ遷移しなくてもいいなら確認フェーズは用意できるかなと思い、作り込んでみた。

投稿一覧に独自リンクを追加する(2013年10月16日 更新)

管理者ページの「投稿一覧」や「固定ページ一覧」の各投稿記事はマウスポインタを移動すると、「編集」や「クイック編集」といったリンクが表示される。このリンクに独自のリンクを追加する方法を調べてみた。

HTML編集モードに定型文ボタンを追加する(2011年6月1日 更新)

投稿編集ページにはビジュアル編集モードとHTML編集モードがあり、それらを拡張するプラグインも多数存在している。そういったプラグインを利用するのもいいのだが、仕組みを理解するのも悪くないはず。というわけで、ここではHTML編集モードのボタンの仕組みと定型文を挿入するボタンを追加する方法を紹介する。

投稿日の検索が自由自在に(2015年3月7日 更新)

WordPress 3.7.0ではWP_Date_Queryクラスが追加され、WP_Queryクラス(もちろん query_posts関数でも)で利用できるようになった。このWP_Date_Queryにより、○年○月○日以前や以降の投稿情報を取得したり、○年○月○日から○年○月○日の範囲の投稿情報を取得したりといったことが、容易に行えるようになった。

プラグインのヘッダー情報を国際化対応しよう(2018年5月27日 更新)

バージョン4.8がリリースされてひと段落。あらためてプラグイン一覧ページを見てみると「Akismet」が「Akismet Anti-Spam (アンチスパム)」と表示されていた。説明文だけじゃなくプラグイン名も国際化対応できたのね。