質問編集履歴

3

さらにさらに試したこと

2022/03/08 11:31

投稿

hotaruika
hotaruika

スコア57

test CHANGED
File without changes
test CHANGED
@@ -25,8 +25,8 @@
25
25
  次のエラーメッセージが表示されます…
26
26
  (このエラーメッセージが出ても、JSは正常に読み込めており動いています)
27
27
  ```
28
- filemtime(): stat failed for https://example.com/wp-content/themes/Simplicity2/js/main.js
28
+ filemtime(): stat failed for https://example.com/wp-content/themes/test-theme/js/main.js
29
- filemtime(): stat failed for https://example.com/wp-content/themes/Simplicity2/js/sidebar.js
29
+ filemtime(): stat failed for https://example.com/wp-content/themes/test-theme/js/sidebar.js
30
30
  ```
31
31
 
32
32
  ### 試したこと
@@ -49,9 +49,9 @@
49
49
  }
50
50
  ```
51
51
  ### さらに試したこと
52
- [こちらのページ](https://wemo.tech/316)に掲載されている下記のコードをもうそのまんまfunctions.phpにコピペし、同じscript.jsを作ってサーバーにアップしましたが、エラーはないもののファイルの更新はされませんでした。
52
+ **[こちらのページ](https://wemo.tech/316)** に掲載されている下記のコードをもうそのまんまfunctions.phpにコピペし、同じscript.jsを作ってサーバーにアップしましたが、エラーはないもののファイルの更新はされませんでした。
53
53
 
54
- ちなみに開発ツールを開いた状態なら、最新のscript.jsで更新できました。
54
+ ちなみに開発ツールを開いた状態なら、最新のscript.jsで更新できましたので、パスはあっているはずです
55
55
 
56
56
  ```php
57
57
  function add_files(){
@@ -72,6 +72,30 @@
72
72
  add_action('wp_enqueue_scripts', 'add_files',1);
73
73
  ```
74
74
 
75
+ ### さらにさらに試したこと
76
+ **[公式サイト](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/wp_enqueue_script)** にある以下のシンプルなコードを実行してもダメです。エラーはありませんが更新はされません。
77
+
78
+ ちなみに開発ツールを開いた状態なら、最新のtest.jsで更新できましたので、パスはあっているはずです。
79
+ ```php
80
+ add_action( 'wp_enqueue_scripts', 'child_add_scripts' );
81
+ function child_add_scripts() {
82
+ wp_register_script(
83
+ 'test',
84
+ get_stylesheet_directory_uri() . 'js/test.js',
85
+ false,
86
+ '1.0',
87
+ true
88
+ );
89
+ wp_enqueue_script( 'test' );
90
+ }
91
+ ```
92
+
93
+ で、出力されたHTMLの<script>タグの`src`を見てみると、上記`child_add_scripts`で指定しているはずの`'1.0'`が抜けていました。
94
+ ```HTML
95
+ <script type="text/javascript" src="https://example.com/wp-content/themes/test-theme/js/test.js" id="test-js"></script>
96
+ ```
97
+
98
+ どうやら **[こちらのStackExchange](https://wordpress.stackexchange.com/questions/338314/enqueue-script-with-version-number-not-working)** によれば、本来`src`に付与されるはずの`var`パラメータが削除されているものと考えられますが、この質問者が解決したプラグイン全停止では、私の場合は解決しませんでした。
75
99
 
76
100
  ### ツールのバージョン
77
101
  PHP v.8.0

2

さらに試したこと

2022/03/08 10:39

投稿

hotaruika
hotaruika

スコア57

test CHANGED
File without changes
test CHANGED
@@ -48,6 +48,29 @@
48
48
  }
49
49
  }
50
50
  ```
51
+ ### さらに試したこと
52
+ [こちらのページ](https://wemo.tech/316)に掲載されている下記のコードをもうそのまんまfunctions.phpにコピペし、同じscript.jsを作ってサーバーにアップしましたが、エラーはないもののファイルの更新はされませんでした。
53
+
54
+ ちなみに開発ツールを開いた状態なら、最新のscript.jsで更新できました。
55
+
56
+ ```php
57
+ function add_files(){
58
+ define("TEMPLATE_DIRE", get_template_directory_uri());
59
+ define("TEMPLATE_PATH", get_template_directory());
60
+
61
+ function wp_css($css_name, $file_path){
62
+ wp_enqueue_style($css_name,TEMPLATE_DIRE.$file_path, array(), date('YmdGis', filemtime(TEMPLATE_PATH.$file_path)));
63
+ }
64
+ function wp_script($script_name, $file_path, $bool = true){
65
+ wp_enqueue_script($script_name,TEMPLATE_DIRE.$file_path, array(), date('YmdGis', filemtime(TEMPLATE_PATH.$file_path)), $bool);
66
+ }
67
+
68
+ //以下のように使う
69
+ wp_script('wemo_script','/js/script.js');
70
+ wp_css('wemo_style','/style.css');
71
+ }
72
+ add_action('wp_enqueue_scripts', 'add_files',1);
73
+ ```
51
74
 
52
75
 
53
76
  ### ツールのバージョン

1

修正しました

2022/03/08 09:42

投稿

hotaruika
hotaruika

スコア57

test CHANGED
File without changes
test CHANGED
@@ -22,9 +22,17 @@
22
22
  }
23
23
  }
24
24
  ```
25
+ 次のエラーメッセージが表示されます…
26
+ (このエラーメッセージが出ても、JSは正常に読み込めており動いています)
27
+ ```
28
+ filemtime(): stat failed for https://example.com/wp-content/themes/Simplicity2/js/main.js
29
+ filemtime(): stat failed for https://example.com/wp-content/themes/Simplicity2/js/sidebar.js
30
+ ```
25
31
 
26
32
  ### 試したこと
33
+ 調べるといくつか出てきまして、
27
- 調べるといくつか出てきまして、唯一効いたのが次の`date`を付けた書き方です。
34
+ ~~唯一効いたのが次の`date`を付けた書き方です。~~
35
+ 次の`date`を付けた書き方も同様に効かずエラーでした。
28
36
  ```php
29
37
  my_enqueue_scripts( ['main', 'sidebar'] );
30
38
  function my_enqueue_scripts( $names ) {
@@ -35,18 +43,12 @@
35
43
  $file,
36
44
  $path,
37
45
  array(),
38
- date( 'YmdHis', filemtime( $path ) ), // こうしたら効きました
46
+ date( 'YmdHis', filemtime( $path ) ),
39
47
  );
40
48
  }
41
49
  }
42
50
  ```
43
- これで確かに最新のファイルを読めるようになったのですが、今度は次のエラーメッセージが表示されるようになってしまいました…
44
- (このエラーメッセージが出ても、JSは正常に動いているみたいです)
45
51
 
46
- ```
47
- filemtime(): stat failed for https://example.com/wp-content/themes/Simplicity2/js/main.js
48
- filemtime(): stat failed for https://example.com/wp-content/themes/Simplicity2/js/sidebar.js
49
- ```
50
52
 
51
53
  ### ツールのバージョン
52
54
  PHP v.8.0