admin管理员组文章数量:1122846
I'm receiving the following filemtime() warning:
Warning: filemtime(): stat failed for /wp-content/plugins/test-social-icons/css/style.css in /app/public/wp-content/plugins/zoo-social-icons/zoo-social-icons.php on line 29
Here's how I'm enqueuing the stylesheet within my plugin:
function test_styles_scripts() {
$dir = plugin_dir_url(__FILE__);
wp_enqueue_style( 'test-style', $dir . 'css/style.css', array(), filemtime( $dir . 'css/style.css' ) );
}
add_action( 'wp_enqueue_scripts', 'test_styles_scripts' );
The stylesheet is correctly enqueuing. Why am I getting this warning?
I'm receiving the following filemtime() warning:
Warning: filemtime(): stat failed for https://testing.local/wp-content/plugins/test-social-icons/css/style.css in /app/public/wp-content/plugins/zoo-social-icons/zoo-social-icons.php on line 29
Here's how I'm enqueuing the stylesheet within my plugin:
function test_styles_scripts() {
$dir = plugin_dir_url(__FILE__);
wp_enqueue_style( 'test-style', $dir . 'css/style.css', array(), filemtime( $dir . 'css/style.css' ) );
}
add_action( 'wp_enqueue_scripts', 'test_styles_scripts' );
The stylesheet is correctly enqueuing. Why am I getting this warning?
Share Improve this question asked Mar 11, 2020 at 13:52 SamSam 2,1863 gold badges30 silver badges59 bronze badges 2 |2 Answers
Reset to default 1Restating the answer from @JacobPeattie in the comments above.
filemtime() requires a path, not a URL
Therefore use plugin_dir_path(__FILE__)
.
The full string for properly printing the cache-busting part of the version number at the end of the asset would be filemtime( plugin_dir_path(__FILE__)
The filemtime()
function in PHP expects a filesystem path, not a URL. However, in your code, you're passing a URL to filemtime(), which causes the stat failed warning.
To fix this issue, you need to use the correct function to get the filesystem path, which is plugin_dir_path()
. This function will give you the path to the plugin directory in the filesystem, which you can then use with filemtime().
how you can fix your code:
Use plugin_dir_path()
to get the filesystem path.
Use plugin_dir_url()
to get the URL for enqueuing the stylesheet.
Try Code
function test_styles_scripts() {
// Get the URL of the plugin directory
$dir_url = plugin_dir_url(__FILE__);
// Get the filesystem path of the plugin directory
$dir_path = plugin_dir_path(__FILE__);
// Enqueue the stylesheet with the file modification time as the version
wp_enqueue_style('test-style', $dir_url . 'css/style.css', array(), filemtime($dir_path . 'css/style.css'));
}
add_action('wp_enqueue_scripts', 'test_styles_scripts');
本文标签: filemtime() warning when enqueuing style within plugin
版权声明:本文标题:filemtime() warning when enqueuing style within plugin 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736302024a1931384.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
plugin_dir_path()
needed to beplugin_dir_url()
. The second one, forfilemtime()
, still needs to beplugin_dir_path()
, becausefilemtime()
requires a path, not a URL, but the browser requires a URL, which is what the first one is for. – Jacob Peattie Commented Mar 11, 2020 at 13:57