$filecs=get_stylesheet_directory_uri().'/css/lucilevi.css';
$filejs=get_stylesheet_directory_uri().'/js/lucilevi.js';
if (file_exists($filecs)) {
echo "CS found";
} else{
echo "CS not found";
}
if (file_exists($filejs)) {
echo "JS found";
} else{
echo "JS not found";
}
Although the path for directory is correct it returns false. Can Anyone explain why this happen? I am new to PHP and Wordpress. Thanks.
$filecs=get_stylesheet_directory_uri().'/css/lucilevi.css';
$filejs=get_stylesheet_directory_uri().'/js/lucilevi.js';
if (file_exists($filecs)) {
echo "CS found";
} else{
echo "CS not found";
}
if (file_exists($filejs)) {
echo "JS found";
} else{
echo "JS not found";
}
Although the path for directory is correct it returns false. Can Anyone explain why this happen? I am new to PHP and Wordpress. Thanks.
Share Improve this question edited Sep 20, 2019 at 7:57 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Sep 20, 2019 at 5:53 Lucifer LeviLucifer Levi 932 silver badges11 bronze badges1 Answer
Reset to default 1file_exists()
lets you check if a file exists on the local server, by passing it a file path. It cannot be used to check for the existence of files via URL, and you're passing it the result of get_stylesheet_directory_uri()
, which returns the URL (http:// etc.) to the file, not the path.
The proper way, these days, to get the path to a theme file is to use get_theme_file_path()
, like so:
$filecs = get_theme_file_path( 'css/lucilevi.css' );
$filejs = get_theme_file_path( 'js/lucilevi.js' );
if ( file_exists( $filecs ) ) {
// etc.
}
if ( file_exists( $filejs ) ) {
// etc.
}
Just be aware that because $filecs
and $filejs
are file paths, you cannot pass them to wp_enqueue_style()
, wp_enqueue_script()
, you still need to pass URLs to those.