2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

wordpress 自作jQueryファイルの外部読み込み

Last updated at Posted at 2016-12-28

wordpress ver.4.6.1

functions.php
<?php

	//ウィジェットを使用できるようにするコード
	if ( function_exists('register_sidebar') )
		register_sidebar();
	
	
	//ここから自作jQueryファイル読み込みコード
	function my_scripts() {

	//wpのjqueryを読み込まない
	wp_deregister_script('jquery');

	//jqueryの読み込み
	wp_enqueue_script( 'jquery', '//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js', "", "20160608", false );

	//自作jqueryファイル
	wp_enqueue_script( 'jquery-kotori', get_bloginfo( 'stylesheet_directory') . '/jquery-kotori.js', array(), false, true );
	}
	add_action( 'wp_enqueue_scripts', 'my_scripts');
	
	
?>

:candy: wp_enqueue_script( 'jquery', '//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js', "", "20160608", false );
google APIのjqueryを読みこまないと、自作jqueryのファイルが利用できないので必ずコードを記述する。

:candy: wp_enqueue_script( $handle(必須), $src(任意), $deps(任意), $ver(任意), $in_footer(任意) );
参考url:http://kwski.net/wordpress/780/
参考url:http://rfs.jp/sb/wordpress/wp-lab/wp_enqueue_script.html
ハンドル名が同じだとエラーとなるので、ハンドル名がかぶらないようにする。

:candy: get_bloginfo( 'stylesheet_directory')
stylesheetがあるフォルダと同じ場所という意味

:candy: add_action( $hook, $function_to_add(関数名自作), $priority, $accepted_args );
特定のアクションに関数をフックする
この関数は add_filter() のエイリアス
このコードでは、
$hook→wp_enqueue_scripts
$function_to_add→my_scripts (function my_scripts() {})
※エイリアス→(意)別の名前で参照するシンボル
参考url:https://wpdocs.osdn.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/add_action

:doughnut: wordpressではjQueryファイルはphpファイルのようにテーマ編集には表示されない。
<jQueryファイルの確認方法 chrome>
・デベロッパーツール→application→scripts
参考url:http://weback.net/utility/1410/

:doughnut: ダッシュボード→カスタマイズ→ウィジェット
表示され、利用可能
試したところ、functions.phpのjQuery外部読み込みでも、headでのscript読み込みでもどちらでもok
前はheadでのscript記述では、使用不可だったのに。。

:poultry_leg: phpコードの書き方に注意 この書き方はエラーとなる

解決方法:一つにまとめる

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?